Angularjs watch service object -



Angularjs watch service object -

why can't watch object in service. ive got simple variable working, object wont work. http://plnkr.co/edit/s4b2g3bas7dwqt3t8xek?p=preview

var app = angular.module('plunker', []); app.service('test', ['$http', '$rootscope', function ($http, $rootscope) { var info = 0; var obj = { "data": 0 }; this.add = function(){ obj.data += 1; console.log('data:', obj); }; this.getdata = function() { homecoming obj; }; }]); app.controller('testcontroller', ['$scope', '$rootscope', '$filter', 'test', function($scope, $rootscope, $filter, test) { //test controller $scope.add = function(){ test.add(); }; $scope.test = test; $scope.$watch('test.getdata()', function(newval){ console.log('data changes into: ', newval) }); }]);

you need pass true lastly parameter of $watch function equality check angular.equals. otherwise reference equality checked.

$scope.$watch('test.getdata()', function(newval){ console.log('data changes into: ', newval) }, true);

duplicate question: $watch object in angular

angularjs service watch

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -