angularjs - Service singleton across all controllers -


if have following :

<div ng-controller="mainctrl">     {{vm.name_from_service}} </div> .... <div ng-controller="mainctrl">     {{vm.name_from_service}} </div> 

lets both pull name_from_service value factory/service , correct assume same service singleton being used across both mainctrl controller instances ?

note mainctrl used twice.


controller code snippet :

$scope.vm.name_from_service = someservice.getname(); 

snippet #2 :

both ng-view , immediate div below use same service retrieve name_from_service, name_from_service update in 1 place.

 <div ng-controller="mainctrl">          section 1         {{vm.name_from_service}}  </div>   <div id="ngviewcontainer" ng-view=""></div> 

also not using asynchronous ajax calls retrieve name_from_service angular should aware of changes.

i've tried using $timeout , $apply() . no dice .

i refactored , separated out unrelated logic separate controller , fixed issue. still bit confused why 1 of 2 values updating evening though both supposedly pulling same service.

yes same, unless digest cycle synchronizations problems. there situations, set variable this:

$scope.vm.name_from_service = someservice.getname(); 

but if work promises , $http calls, sometime have watch changes in singletons manually using $watch. that's situation both controllers can have different values.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -