angularjs - $watch not called when model property changes -


i starting angular , trying understand $watch function in scope api. here link js fiddle :

js fiddle watch demo not working

var myapp = angular.module('myapp1', []); angular.element(document).ready(function() {          angular.bootstrap(document, ['myapp1']);     }); function mycontroller($scope){          var mc = this;          mc.focus = {             'name' :false,             'email':false            };     $scope.$watch(mc.focus,function(oldval,newval){      console.log(oldval);      console.log(newval);     },false);     mc.allfilled = false;     mc.setfocus = function(prop) {        mc.focus[prop] = true;     }; };     myapp.controller("mycontroller",mycontroller); 

what want :

1) have 4 inputs have included 1 in above link brevity , demo.

2) each of inputs has corresponding keys in focus object initial values false.

3) changing property value true in focus object whenever appropriate input element focused using setfocus() function

4) properties in focus object have truthy values want change allfilled property true.

what trying here : achieving want watching focus object , in listener checking if properties have value true. if yes change allfilled property/or other stuff well.

i know maybe can execute above logic in setfocus() want try $watch.
1)is possible doing using $watch?
2) why $watch not called?
3)what best way in angular?

there 2 errors in code.

  1. a scope.$watch documentation says, first argument can string or function. not supposed pass variables, unless value 1 of these.

  2. since watching changes in object properties, third argument $watch should set true:

false - compares object refenece equality, eg. oldval === newval. since object same, changes not found.

true - looks changes in properties in object. since values of properties changed, watcher fired.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -