角JS在指令属性观察 [英] Angular JS observe on directive attribute
本文介绍了角JS在指令属性观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何能角度看JS为了接受角度值自定义指令属性被绑定
下面是我到目前为止有:
<砖标题=睡眠时间的数据值={{sleepHistory.averageSleepTime}}/>app.directive('瓦',[功能(){
返回{
限制:'E',
链接:功能(范围,元素,ATTRS){
VAR标题= attrs.title; ATTRS。观察$('dataValue',函数(VAL){
VAR数据= VAL; 的console.log(数据); VAR DOM =
< DIV>中+
&所述; P>中+标题+< / P>中+
&所述; P>中+资料+&下; / P>中+
< / DIV $(元件).append($(dom.trim()));
});
}
};
}]);
但观测值是回来为未定义
解决方案
从<一个href=\"http://docs.angularjs.org/api/ng.%24compile.directive.Attributes\">http://docs.angularjs.org/api/ng.$compile.directive.Attributes:
所有这些都被视为等同于角:
块引用>&LT;跨度NG:绑定=一个NG绑定=数据-NG-绑定=一个X-NG-绑定=一个&GT;
所以属性
数据值
标准化为值
所以,这是你想要什么:
ATTRS。观察$('值',函数(VAL){
How can angular js watch attributes on custom directive in order to accept angular values to be bind
Here is what I have so far:
<tile title="Sleep Duration" data-value="{{sleepHistory.averageSleepTime}}"/> app.directive('tile', [function() { return { restrict: 'E', link: function(scope, element, attrs) { var title = attrs.title; attrs.$observe('dataValue', function(val) { var data = val; console.log(data); var dom = "<div>" + "<p>" + title + "</p>" + "<p>" + data + "</p>" + "</div"; $(element).append($(dom.trim())); }); } }; }]);
but the observed value is coming back as undefined
解决方案From http://docs.angularjs.org/api/ng.$compile.directive.Attributes:
all of these are treated as equivalent in Angular:
<span ng:bind="a" ng-bind="a" data-ng-bind="a" x-ng-bind="a">
So the attribute
data-value
normalizes tovalue
So, this is what you want:
attrs.$observe('value', function(val) {
这篇关于角JS在指令属性观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文