angularjs - Angular TypeError: Cannot read property 'then' of undefined -
angularjs - Angular TypeError: Cannot read property 'then' of undefined -
i have info service this:
this.myfunction= function(callback) { var url = rooturl + "path1/path2/service.json"; var promise = $http.get(url); promise.then(function(payload){ homecoming callback(payload); }); homecoming promise; }
it called in controller initialize stuff:
dataservice.myfunction(function(data) { if(data.statustext !== "ok"){ $scope.$worked= false; }else{ $scope.$worked= true; } }
and "typeerror: cannot read property 'then' of undefined". console.log(data) in callback shows 200 "ok" response , info expect. have searched error , due not returning promise in service. however, i'm returning promise. setting on controller scope in callback causes error.
angular version: angularjs v1.3.0-rc.2
thanks!
you don't need homecoming promise in case, because using callback. callbacks , promises 2 ends of spectrum. can accomplish want this.
if want utilize callback can leave controller code.
this.myfunction= function(callback) { var url = rooturl + "path1/path2/service.json"; $http.get(url).then(function(response) { callback(response.data); }); }
or if want utilize promises
this.myfunction= function() { var url = rooturl + "path1/path2/service.json"; homecoming $http.get(url).then(function(response) { homecoming response.data; }); } dataservice.myfunction().then(function(data) { if(data.statustext !== "ok"){ $scope.$worked = false; } else { $scope.$worked = true; } });
angularjs
Comments
Post a Comment