Saturday, 15 May 2010

javascript - Watching ngModel of an element from attribute directive -



javascript - Watching ngModel of an element from attribute directive -

i want watch model within directive place attribute on input element:

<input id="mymodel_headline" name="mymodel[headline]" ng-model="mymodel.headline" sps-watch="true" type="text">

if i'm reading docs right should able value either through link function's attrs parameter, or requiring ngmodel. however, neither of these approaches working:

app.directive('spswatch', function() { homecoming { require: "ngmodel", link: function(scope, element, attrs, ngmodel) { console.log("directive called"); console.log(attrs.ngmodel); scope.$watch(scope[attrs.ngmodel], function(result) { console.log("watching via attribute"); console.log(result); }); scope.$watch(ngmodel, function(result) { console.log("watching via ngmodel"); console.log(result) }); } }; });

they seem run 1 time only, when directive first created.

plunkr here

you can retrieve value of model via ngmodel.$viewvalue. homecoming anonymous function in $watch expression, , changes in model trigger $watch function:

scope.$watch(function () { homecoming ngmodel.$viewvalue; }, function(result) { console.log("watching via ngmodel"); console.log(result) });

javascript angularjs angularjs-directive angularjs-scope

No comments:

Post a Comment