angularjs – Angular JS观察指令属性

前端之家收集整理的这篇文章主要介绍了angularjs – Angular JS观察指令属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
角度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

all 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) {
原文链接:https://www.f2er.com/angularjs/144484.html

猜你在找的Angularjs相关文章