Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  34] [ 2]  / answers: 1 / hits: 93330  / 10 Years ago, mon, march 3, 2014, 12:00:00

I am new to AngularJS and find it very interesting, but I am a bit unclear about the following situation.



app.factory('deleteFac', function($http){

var factory = {};

factory.edit = function(id){
$http.get('?controller=store&action=getDetail&id=' + id).
success(function(data, status){
/**
got an error on the following
when i use return data; and i get data undefined
in the controller which i get it because its doing a ajax call
you don't get data until the call first.
**/
$scope.detail = data;
})
}

return factory;
})


I am getting error when I assign to $scope and use return data, is there anyway I can assign the return data to the $scope?


More From » angularjs

 Answers
25

You don't typically use $scope inside a factory, service or provider. Usually, you would return the promise (returned by $http) and then handle the promise in a controller (where you do have $scope).



factory.edit = function(id){
return $http.get('?controller=store&action=getDetail&id=' + id);
}


Controller function:



$scope.edit = function(id) {

deleteFac.edit(id).then(function(response) {
$scope.something = response.model;
});
}

[#72183] Sunday, March 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
finn

Total Points: 154
Total Questions: 88
Total Answers: 82

Location: Lithuania
Member since Mon, Nov 16, 2020
4 Years ago
;