javascript - AngularJS Load Data from Service -
i having problem getting data service populated view. have service defined such
app.factory('nukeservice', function($rootscope, $http) { var nukeservice = {}; nukeservice.nuke = {}; //gets list of nuclear weapons nukeservice.getnukes = function() { $http.get('nukes/nukes.json') .success(function(data) { nukeservice.nukes = data; }); return nukeservice.nukes; }; return nukeservice; }); and controller
function navigationctrl($scope, $http, nukeservice){ /*$http.get('nukes/nukes.json').success(function(data) { $scope.nukes = data; });*/ $scope.nukes = nukeservice.getnukes(); } if use $http.get controller data populates fine, however, if try call data service, nothing. understand query asynchronous having hard time understanding how populate $scope variable once data returned. use $rootscope broadcast event , listen in controller not seem correct way accomplish this. appreciate advice on how correct way.
i think should solve problem
app.factory('nukeservice', function($rootscope, $http) { var nukeservice = {}; nukeservice.data = {}; //gets list of nuclear weapons nukeservice.getnukes = function() { $http.get('nukes/nukes.json') .success(function(data) { nukeservice.data.nukes = data; }); return nukeservice.data; }; return nukeservice; }); function navigationctrl($scope, $http, nukeservice){ $scope.data = nukeservice.getnukes(); //then refer nukes list `data.nukes` } this problem object reference.
when calls nukeservice.getnukes() getting reference object a variable $scope.nukes refers memory location.
after remote server call when set nukeservice.nukes = data; not changing object a instead changing nukeservice.nukes referencing object a object b. $scope.nukes not know reassignment , still points object a.
my solution in case pass object a property data , change data property instead of changing reference a
Comments
Post a Comment