角度js如何在自定义指令上观察属性,以接受角度值来绑定
这是我到目前为止
<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())); }); } }; }]);
但是观察到的价值是未定义的
从
http://docs.angularjs.org/api/ng.$compile.directive.Attributes:
原文链接:https://www.f2er.com/angularjs/144484.htmlall of these are treated as equivalent in Angular:
<span ng:bind="a" ng-bind="a" data-ng-bind="a" x-ng-bind="a">
所以属性数据值归一化为值
所以,这就是你想要的:
attrs.$observe('value',function(val) {