Using AngularJS. I have a directive that I want to have two-way data binding. The directive will have the attribute "activate". Initially, the βactivateβ value will be β1β.
The directory link function checks if the "activate" value is "1". If so, it will change βactivateβ to 0, but will do some other things.
Later, if I want the directive to do something again in the controller, I will again change the "activate" to "1". Since the directive has a clock, it will repeat the loop.
Unfortunately, every time I do this, I get the expression "0", used with the directive "testDirective", does not lend itself to purpose! "or" Inappropriate model expression: 1 (directive: testDirective). "
Here is the HTML:
<body ng-app="app"> <test-directive activate="1"></test-directive> </body>
Here is the 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(); } }); } } });
Here it is on jsFiddle: http://jsfiddle.net/justbn/mgSpY/3/
Why can't I change the value stored in the directive attribute? I use two way binding.
The docs say: "If the property of the parent area does not exist, it throws an exception NON_ASSIGNABLE_MODEL_EXPRESSION."
NOTE. The content for the update correctly displays the value "activate". However, the value "activate" in "" is not updated.
However, this does not make any sense to me, since there is a property of the parent scope.
Any ideas?