javascript - Data flow issue with AngularJS -


i have function inside directive makes query (and gets results, according console). problem can't seem able store results factory, , pass them controller.

this directive:

scope.getversions = function(release) {       if (angular.isundefined(release)) return;        musicinfoservice.getreleaseversions(release.id)         .success(function(data) {           dataservice = data.versions;           console.log(dataservice);         });  }; 

the console shows dataservice contains array results. then, try store factory:

app.factory('dataservice', [function(){   return { items: [] }; }]); 

and call in controller:

function versioncontroller($scope, dataservice) {   $scope.versions = dataservice.items;   console.log($scope.versions); } 

but both items , $scope.versions come empty array. did miss something?

you should use backing field store data, , use setter , geter functions writing , reading respectively:

app.factory('dataservice', [function(){    var _items={};    return {            setitems:function(value){              _items=value;           },           getitems:function(){              return _items;           }   }; }]); 

and setting data:

 musicinfoservice.getreleaseversions(release.id)         .success(function(data) {           dataservice.setitems(data.versions);           console.log(dataservice);         }); 

and reading:

function versioncontroller($scope, dataservice) {   $scope.versions = dataservice.getitems();   console.log($scope.versions); } 

see demo plunk.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -