本文介绍了隔离范围突破ATTRS。$观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我漂亮的removeDialog指令,因为这个问题的结果是:

So I have my nice removeDialog directive as a result of this question:

Updating内部指令ATTRS价值 - 如何做到这一点在AngularJS

而现在我开始isoleted范围播放。我注意到的第一件事就是将孤立的范围爆发ATTRS。$观察。我没有收到通知时的触发改变。

And now I started playing with isoleted scope. The first thing I've noticed is that adding isolated scope broke attrs.$observe. I do not receive notifications when trigger is changed.

homesApp.directive("removeDialog", function ($parse) {
    return {
        scope: {

        },
        restrict: 'A',
        link: function (scope, element, attrs, controller) {
            angular.element(element).on('hidden.bs.modal', function () {
                scope.$apply(function () {
                    scope.cancelRemove();
                });
            });
            attrs.$observe('trigger', function (newValue) {
                if (newValue) {
                    angular.element(element).modal('show');
                } else {
                    angular.element(element).modal('hide');
                }
            });
        },
        controller: 'DeleteController'
    };
});

您能解释一下为什么吗?

Could you elaborate why?

推荐答案

触发器的含量未绑定到外部范围了。需要声明它在你的范围内分离:

The content of trigger is not bound to the outer scope anymore. You need to declare it in your isolate scope:

scope: {
    trigger: '='
}

这将绑定 scope.trigger 您在应用该指令的元素定义实际EX pression。

This will bind scope.trigger to the actual expression that you define on the element in which the directive is applied.

这样, ATTRS。观察$('扳机',函数(为newValue){...} 应更改为

scope.$watch('trigger', function (newValue) {
            if (newValue) {
                angular.element(element).modal('show');
            } else {
                angular.element(element).modal('hide');
            }
        });

这篇关于隔离范围突破ATTRS。$观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:41