angularjs - How to modify a scope's value within a directive -
within directive, i'd modify expression's value when button clicked. below code modifying value locally within directive. i'd change value outside of directive well. how can accomplish that?
scope: false, link: function (scope, el, attrs) { //toggle state when clicked el.bind('click', function () { scope[attrs.ngmodel] = !scope[attrs.ngmodel]; });
plunker: http://plnkr.co/edit/xqybnz5bhlp844kxjxks?p=preview
the problems binding expression in ng-model. since has '.' in it, need use $parse
, set value correctly. see documentation here
something like
var getter=$parse(attrs.ngmodel); var setter=getter.assign; setter(scope,!getter(scope));
see updated plunkr here http://plnkr.co/edit/vae43y5cagcf8jq5alay?p=preview
basically setting
scope[attrs.ngmodel] = !scope[attrs.ngmodel]
creates new property user.active
. set should scope['user']['active'];
Comments
Post a Comment