如何使用ng-style访问当前元素 [英] How to access to the current element with ng-style

查看:117
本文介绍了如何使用ng-style访问当前元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用ng-style时,我想访问当前元素的css属性

I'd like to get access to the current element css properties when I use ng-style

由于以下元素:

<div ng-style="changeWidth()">

访问其CSS属性

$scope.changeWidth = function(element) {
    return {
       width: element.width() / 2
    }
};

有人知道实现此目标的最佳方法吗?

Someone knows the best way to achieve that?

推荐答案

使用指令:

.directive("getStyle",function(){
  return{
    link:function(scope,element){
      element[0].style.border = "5px dotted #090";
    }        
  }      
});

示例: http://plnkr.co/edit/izOrEi?p=preview

又一个(定向作用域交互): http://plnkr.co/edit/uL7N1j ?p =预览

One more (directive-scope interaction): http://plnkr.co/edit/uL7N1j?p=preview

这篇关于如何使用ng-style访问当前元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆