I've tried other answers with the same problem, but none of them worked. I have the issue where I'm calling my factory and I get the undefined
return.
Controller (main.js):
app.controller('MainCtrl', ['$scope','beamAPI', function($scope, beamAPI){
$scope.debug = 'Debug True';
$scope.beamFollowers = 1;
console.log(beamAPI('amtraxtge'));
}]);
Factory (beam.js):
app.factory('beamAPI', function($http) {
var APIuser = {};
APIuser = function(user) {
$http.get('https://beam.pro/api/v1/channels/' + user).
then(function(res){
console.log(res.data);
return res.data;
});
}
return APIuser;
});
Console:
undefined main.js:4
► Object beam.js:6
Your APIuser()
doesn't return anything. You need to return the promise and in controller wait for promise to resolve before assigning or logging any of the data
In factory
APIuser = function(user) {
// return $http promise
return $http.get('https://beam.pro/api/v1/channels/' + user).
then(function(res){
console.log('Factory log',res.data);
return res.data;
});
}
In controller
beamAPI('amtraxtge').then(function(data){
$scope.someProperty = data;
console.log('Controller log',data);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.