从指令中更改属性

Jus*_*oel 6 angularjs

使用AngularJS.我有一个指令,我希望有两种方式的数据绑定.该指令将具有一个名为"activate"的属性.最初,"激活"的值将为"1".

该指令的链接功能将检查"activate"是否等于"1".如果是这样,它会将"激活"改为0,但做其他一些事情.

之后,如果我希望指令再次执行某些操作,在控制器中,我会再次将"激活"更改为"1".由于指令有手表,它将重复循环.

不幸的是,每次我这样做,我得到的'表达'0'与指令'testDirective'一起使用是不可分配的!" 或"不可分配的模型表达式:1(指令:testDirective)".

这是HTML:

<body ng-app="app">
    <test-directive activate="1"></test-directive>    
</body>
Run Code Online (Sandbox Code Playgroud)

这是JS:

var app = angular.module('app', []);


app.directive('testDirective', function() {
    return {
        restrict: 'E',
        scope: {
            activate : '='
        },


        link: function( scope, elem, attrs, controller ) {
            var el = elem[0];

            var updateContent = function() {
                el.innerText = 'Activate=' + scope.activate;
            };

            updateContent();
            attrs.$observe( 'activate', function() {
                console.log('Activate=' + scope.activate);
                if( scope.activate == '1') {
                    scope.activate = '0'
                    updateContent();
                }
            });
        }
    }
});      
Run Code Online (Sandbox Code Playgroud)

这里是jsFiddle:http://jsfiddle.net/justbn/mgSpY/3/

为什么我不能更改存储在指令属性中的值?我正在使用双向绑定.

文档说"如果父作用域属性不存在,它将抛出NON_ASSIGNABLE_MODEL_EXPRESSION异常."

注意:更新内容正确显示"激活"的值.但是,""中的"激活"值不会更新.

然而,这对我来说没有意义,因为父范围属性存在.

有任何想法吗?

Den*_*Luz 12

虽然我同意使用$watch而不是attrs.$observe这不是你得到错误信息的主要原因.

问题是您正在尝试为不可赋值的表达式赋值 - 正如错误消息告诉您: Non-assignable model expression: 1 (directive: testDirective)

在这种情况下,不可分配的表达式是数字"1"

<test-directive activate="1">
Run Code Online (Sandbox Code Playgroud)

您设法将初始值(1)传递给指令,但是当指令尝试更新该值时,无法更改属性activate - 因为它是一个数字.

因此,您需要做的是将其更改为变量,以便稍后更新该值.

请参阅下面的代码,我通过控制器在$ scope中初始化一个名为activate.initialValue的变量.

我也用过$watch而不是attrs.$observe.

我添加了$timeoutjust来模拟一个事件,以便在2秒后更改激活值.

HTML

<body ng-app="app" ng-controller="appCtrl">
    <test-directive activate="activate.initialValue"></test-directive>    
</body>
Run Code Online (Sandbox Code Playgroud)

JS

var app = angular.module('app', []);

app.controller('appCtrl', function($scope){
    $scope.activate = {
        initialValue : 1
    }
});

var app = angular.module('app', []);

app.controller('appCtrl', function($scope){
    $scope.activate = {
        initialValue : 2
    }
});

app.directive('testDirective', function($timeout) {
    return {
        restrict: 'E',
        scope: {
            activate : '='
        },      
        link: function(scope, elem, attrs) {
            scope.$watch('activate', function(newValue, oldValue){
               console.log('activate has changed', newValue);
            });

             $timeout(function(){ 
                    scope.activate = 0;     
                }, 2000);
        },
        template: "{{activate}}"
    }
});   
Run Code Online (Sandbox Code Playgroud)

你也可以看到在这里工作(http://jsfiddle.net/mgSpY/63/).

这里是关于它的AngularJS官方文档(http://docs.angularjs.org/error/ngModel:nonassign)