Angularjs 在Angular中,如何在属性值更改时触发的指令中设置手表?

Angularjs 在Angular中,如何在属性值更改时触发的指令中设置手表?,angularjs,Angularjs,如果我有这样的指令调用: <mydirective data-pid="data.Id"></mydirective> app.directive('mydirective', function($compile) { return { restrict: 'E', scope: { pid: '=' }, link: function (scope, element, at

如果我有这样的指令调用:

<mydirective data-pid="data.Id"></mydirective>
app.directive('mydirective', function($compile) {
    return {
        restrict: 'E',
        scope: {
            pid: '='
        },
        link: function (scope, element, attrs) {
            scope.$watch('pid', function(newValue, oldValue) {
                if (newValue !== oldValue) {
                    console.log('pid changed', newValue);
                }
            });
        }
    }
});

然后,在指令的链接区域中,如何查看此属性并在其值发生变化时执行操作?

您可以执行以下操作:

<mydirective data-pid="data.Id"></mydirective>
app.directive('mydirective', function($compile) {
    return {
        restrict: 'E',
        scope: {
            pid: '='
        },
        link: function (scope, element, attrs) {
            scope.$watch('pid', function(newValue, oldValue) {
                if (newValue !== oldValue) {
                    console.log('pid changed', newValue);
                }
            });
        }
    }
});
请参阅此演示:。 使用作用域:{pid:'='}可以

在本地作用域属性和通过attr属性值定义的name的父作用域属性之间设置双向绑定


引用第行。

您能给我看一下您的指令代码吗?请参阅可能对您有帮助的链接。我有一个非常类似的问题,现在将发布。