Angular JS 观察指令属性 [英] Angular JS observe on directive attribute

查看:27
本文介绍了Angular JS 观察指令属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angular js 如何监视自定义指令上的属性以接受要绑定的角度值

这是我目前所拥有的:

app.directive('tile', [function() {返回 {限制:'E',链接:函数(范围,元素,属性){var title = attrs.title;attrs.$observe('dataValue', function(val) {var 数据 = val;控制台日志(数据);变量 dom =

"+<p>"+ 标题 + "</p>"+<p>"+ 数据 + "</p>"+"</div";$(element).append($(dom.trim()));});}};}]);

但观察到的值返回未定义

解决方案

来自 https://docs.angularjs.org/api/ng/type/$compile.directive.Attributes:

<块引用>

所有这些在 Angular 中都被视为等价的:

所以属性 data-value 归一化为 value

所以,这就是你想要的:

attrs.$observe('value', function(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 https://docs.angularjs.org/api/ng/type/$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 to value

So, this is what you want:

attrs.$observe('value', function(val) {

这篇关于Angular JS 观察指令属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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