angularjs - Fetching an object from a service which hasn't yet been created -


folks:

i have 2 controllers, ctrla , ctrlb - both unrelated each other within same page.

ctrla queries end point , returns json object tags, passed service method myservice.savetags(tags) store object.

ctrlb needs populate $scope variable $scope.tags fetching tags object created via ctrla.

the service:

.factory('myservice', function($http, $q, $window) {   var myservicefactory = {};   var savedtags = {};  // ..other methods..    myservicefactory.savetags = function(tags) {     if(!savedtags.tags){       console.log('saving tags..');       savedtags.tags = tags;     }   };    myservicefactory.getsavedtags = function() {     console.log('returning tags..');     return savedtags.tags;   };    return myservicefactory; }) 

this issue appears ctrlb gets called first, when $scope.savedtags = myservice.getsavedtags(); runs, returns undefined.

question: angular n00b here - best way fetch tags after ctrla has populated object?

you can use watch on getsavedtags service method inside ctrlb know when populated. like

$scope.$watch(function() { return myservice.getsavedtags() },function(newvalue) {    if(newvalue) {       $scope.savedtags = newvalue;    } }); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -