Using AngularJS. I have a directive that I want to have two way data binding. The directive will have an attribute called "activate". Initially, the value of "activate" will be "1".
The directive's link function will check if "activate" is equal to "1". If so, it will change "activate" to 0, but do some other stuff.
Later, if I want the directive to do some stuff again, in the controller, I will change "activate" to "1" again. Since the directive has the watch, it will repeat the cycle.
Unfortunately, every time I do this, I get "Expression '0' used with directive 'testDirective' is non-assignable!" or "Non-assignable model expression: 1 (directive: testDirective)".
Here is HTML:
<body ng-app="app">
<test-directive activate="1"></test-directive>
</body>
Here is 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's attribute? I'm using 2 way binding.
The docs say " If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception."
NOTE: The update content properly shows the value of "activate". However, the value of "activate" in "" does not update.
However that makes no sense to me as the parent scope property DOES exist.
Any ideas?