AngularJS checkbox inside directive -


so have directive has checkbox in it. need make web service call when value of checkbox changed. how can value of checkbox when clicked? want checkbox confined scope of directive.

mymodule.directive('test', function() {     return {         restrict: 'a',         replace: true,         scope:{},         template:                '<div>'+                 '<input type="checkbox" ng-click="toggleroomlock()" name="lockroom" value="lock" ng-model="lockroom">'+             '</div>',         link: function(scope, element, attrs) {             scope.toggleroomlock = function(){                 //get value of checkbox here             };         }     } } 

i have tried getting value using scope.lockroom getting undefined. suggestions?

the easiest way remove ng-click attribute template , put watch on model. change link function to:

function (scope, element, attrs) {     scope.lockroom = false; // initialize unchecked      scope.$watch('lockroom', function (value) {         // value contain either true or false depending on checked state     }); } 

$watch allows listen changes on models (basically objects added scope).

example plunker (value logged console).


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -