javascript - AngularJS losing reference to service variable on re-assignment? -
i didn't know better way phrase question. i've written basic service used 2 controllers.
jsfiddle: http://jsfiddle.net/aditya/2nd8u/2/
clicking 'notify' works expected; adds notification array. 'reset' breaks it. clicking either button after 'reset' doesn't anything. know what's going on here?
ps. think has angular losing reference since notifs being re-assigned (technically), wrote getters , setters, emptying array involves pop()ing till it's empty, doesn't seem efficient.
plunkr if jsfiddle down: http://plnkr.co/edit/mzflljfxcwsxm5kdebhc
i changed service this:
.factory("notificationservice", function(){     var notifications = [];     return {         notifs: notifications,         clear: function(){             angular.copy([], notifications);         },         get: function(){              return notifs;          }     } })   and controller :
$scope.reset = function(){         console.log("reset");         notificationservice.clear();         console.log(notificationservice);     }   and works me.
naturally should little bit tidier, in instead of notifs should have get, , add , remove method, wanted show code changed. angular.copy method makes sure changes made within angular's lifecycle.
as can't bind variable methods, this:
$scope.getnotifications = notificationservice.get;
that should work.
Comments
Post a Comment