Updating a view in AngularJS using $watch on a service -



Updating a view in AngularJS using $watch on a service -

i'm newbie angular , i'm struggling understand how views updated scope changes. trying update header in app using angular js based on whether user logged in. info returned login service.

i have distilled problem 2 plunkers, 1 working , 1 not.

in order work have assign loginservice variable on scope of headerctrl.

angular.module('app', []) .controller('headerctrl', function ($scope, loginservice) { $scope.loginservice = loginservice; $scope.$watch('loginservice.isloggedin()', function(newval) { $scope.isloggedin = newval; });

here working version http://plnkr.co/edit/kbze9n?p=preview

now if remove reference loginservice in scope of headerctrl , utilize injected service in watch directly, view stops updating. demonstrated here http://plnkr.co/edit/ijfs2w?p=preview

can explain me why sec case doesn't work? i've read bad thought have watches within controller i'm open improve solutions.

because $watch watches scope variables in scope calling from. in sec example, $watch looking loginservice variable in scope, of course of study not exists.

angularjs angularjs-scope

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -