json - How can I pass data from a service into my controller? -
okay right messing around ionic framework , learning angularjs @ same time. came across $q , async calls, can't seem right. want able parse json file have set using getjsonspecials
pass getdata
pass controller specialsctrl
can attach $scope
. know not understanding promises correctly because inside specialservice
undefined. can data fine other 2 serivces, when try passing specialservice
seems crumble in turn ends undefined in controller. maybe not going right way? there best practices of doing kind of thing?
angular.module('starter.controllers', []) .controller('specialsctrl', function ($scope, specialservice) { $scope.specials = specialservice.all(); console.log("specials controller: got data", $scope.specials); }) //create methods access specials inside controller in inject in .factory('specialservice', function (getdata) { var specials = getdata.getspecials(); console.log("dataaaa: ", specials); return { // return specials all: function () { console.log("inside return specials: ", specials); return specials; }, getspecialwithid : function (specialid) { // simple index lookup return specials[i]; } } } }) .factory('getdata', function(getjsonspecials) { return { getspecials : function() { getjsonspecials.retrievedata().then(function (data) { console.log("got json data", data); return data; }, function (status) { alert("error getting specicals", status); console.log("error getting specicals", status); }); } } }) //asynchronously specials json file .factory('getjsonspecials', function ($q, $http) { return { retrievedata : function() { var deferred = $q.defer(); $http.get('js/specials.json').success(function (data, status) { deferred.resolve(data); }).error(function (status) { deferred.reject(status); console.log("error in handling json!"); }); return deferred.promise; } } })
the reason have overly complicated because in end want able share data controller display specific specials' properties in new view.
.controller('detailctrl', function ($scope, $stateparams, jsonspecials, $firebaseauth) { $scope.id = parseint($stateparams.specialid); $scope.special = jsonspecials.getspecialwithid($scope.id); })
it seems on complicating things bit. you're passing data around factories no clear reason. think 3 factories combined one. maybe try like..
index.html
<!doctype html> <html ng-app="foobar"> <head> <link rel="stylesheet" href="style.css"> </head> <body> <div ng-controller="specialsctrl"> {{specials}} </div> <div ng-controller="anothercontroller"> {{specials}} </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src="script.js"></script> </body> </html>
script.js
angular.module('foobar', []) .controller('specialsctrl', function ($scope, jsonspecials) { jsonspecials.retrievedata().then(function(data){ $scope.specials = data; }); }) .controller('anothercontroller', function ($scope, jsonspecials) { jsonspecials.retrievedata().then(function(data){ $scope.specials = data; }); }) //$http returns promise anyway don't need $q .factory('jsonspecials', function ($http){ return { retrievedata : function() { return $http .get('js/specials.json') .error(function (status) { console.log("error in handling json!"); }); } } });
Comments
Post a Comment