我的控制器从工厂获取{“$$ state”:{“status”:0}}而不是工厂应该传递的值。我如何从工厂获得价值?
这是我的controller.js代码:
.controller('omnipayLoginCtrl', ['$scope', '$stateParams', '$location', 'userFactory', function ($scope, $stateParams, $location, userFactory) {
$scope.userdata = {}
$scope.enterlogin = function(usern,pass) {
alert('get = '+ JSON.stringify(userFactory.getUser(usern)) );
}
}])
这是我的services.js代码:
.factory('userFactory', ['$http', function($http) {
var users = [];
return {
getUser: function(uname){
return $http.get("http://10.10.9.169/UserService/api/users/" + uname).then(function(response){
if(response.data == null)
{
users = null;
}
else
{
users = response.data;
}
return users;
});
}
};
}])
答案 0 :(得分:0)
尝试在控制器而不是工厂中捕获承诺。
.factory('userFactory', ['$http', function($http) {
var users = [];
return {
getUser: function(uname) {
return $http.get("http://10.10.9.169/UserService/api/users/" + uname)
}
};
}])
.controller('omnipayLoginCtrl', ['$scope', '$stateParams', '$location', 'userFactory', function($scope, $stateParams, $location, userFactory) {
$scope.userdata = {}
$scope.enterlogin = function(usern, pass) {
userFactory.getUser(usern).then(function(response) {
alert('get = ' + JSON.stringify(response.data));
})
}
}])