javascript - angularjs watch for change in scope variable made from another controller -
i have following service
function pathfactory() { this.path = null; this.setpath = function(path) { this.path = path; } return this; }
my navigation controller:
function navcontroller($scope, pathfactory) { list = document.getelementsbytagname("ul"); list = list[0]; var items = list.getelementsbytagname("li"); for(var = 0 ; < items.length ; i++) { items[i].setattribute("navindex", i); items[i].addeventlistener("click", navclick); } function navclick(e) { switch(e.path[1].hash) { case "#home": pathfactory.setpath("home"); break; case "#bots": pathfactory.setpath("bots"); break; case "#servers": pathfactory.setpath("servers"); break; case "#status": pathfactory.setpath("status"); break; } } }
and content controller:
function contentcontroller($scope, pathfactory) { $scope.$watch(function() { return pathfactory.path; }, function (newvalue) { alert("changed value"); }); }
the $scope.$watch isn't running function should value of pathfactory.path changed navigation controller, way can setup working?
cheers guys.
if want angularjs calls $watch()
method, have call $apply()
method manualy.
angulars two-way-databinding works, because changes in $scope
calls $apply
method. way angular knows has changed.
try put line of code $scope.$apply();
@ end of navclick
function.
but should use directive event listeners and/or dom-manipulation
Comments
Post a Comment