Reputation: 15
I try to use $scope inside a factory to stock a value but i can't.
I try also to push the value into an array, it doesn't work.
I just want to copy a callback value into a variable to return it from a factory.
This is a sample :
var cpy = {};
$http.get('/api/things/')
.success(function (data){
cpy = data;
})
.error(function (err){
});
console.log(cpy);
Thanks for your help.
Upvotes: 1
Views: 827
Reputation: 15
Thanks to Maxim, Okazari and user3227295.
my factory look like this :
angular.module('feedbackApp').factory('Data', function ($http, $q, Auth) {
$http.get('/api/things/')
.success(function (data){
cpy = data;
})
.error(function (err){
});
return {first : true};
});
I just want to check if cpy was not empty to return false and return true for empty variable.
Sorry to asked the question without much precisions.
Thank you.
Upvotes: 0
Reputation: 4597
That was almost what you need.
You factory should look like :
var getCopy = function(){
return $http.get('/api/things/');
}
And in your controller you get it like this :
myfactory.getCopy().success(function(data){
$scope.myscopedvar = data;
});
If you want some binding in your scope it'll always be done in the controller. If it's an asynchronous call, it's the responsibility of your controller to know what to do when the $http promise resolve.
Hope it helped, if you want more explanation feel free to ask.
Upvotes: 0