在调用restful web api2时,Angular控制器返回undefined结果。我看到了许多有关此问题的帖子,但似乎没有解决我的问题。我有一个控制器,用它调用restful web api2。使用fiddler我可以看到调用已经成功并返回200响应,但我似乎永远无法得到结果,我知道这是异步调用。以下是工厂和控制器的代码。我还试图将回调显式地发送到工厂中,但此方法甚至导致未定义对象结果,根本不能正常工作。我真的陷入了困境,而且我刚刚开始接触Angularjs,请问是什么问题?目前,我已将控制器调用连接到按钮进行显式测试。
**FACTORY NO EXPLICIT CALL-BACK**
casenoteApp.factory('MyFactory', function ($http) {
var factory = {};
var urlBase = 'http://xxxxxx/api/Client';
factory.getClients = function (userLogin) {
return $http.get(urlBase + '/' + userLogin);
};
return factory;
});
**CONTROLLER NO EXPLICIT CALL-BACK**
casenoteApp.controller('MyController', MyController);
function MyController($scope, MyFactory) {
$scope.addCustomer = function () {
MyFactory.getClients(123456)
.success(function (clients) {
var curCust = clients;
$scope.status = clients.last_name;
})
.error(function (error) {
$scope.status = 'Unable to load client data: ' + error.message;
});
}
}
**FACTORY PASSING IN CALL-BACK**
casenoteApp.factory('MyFactory', function ($http) {
var factory = {};
var urlBase = 'http://xxxxxx/api/Client';
factory.getClients = function (userLogin, callback) {
return $http.get(urlBase + '/' + userLogin).success(callback)
}
return factory;
});
**CONTROLLER PASSING IN CALL-BACK**
casenoteApp.controller('MyController', MyController);
function MyController($scope, MyFactory) {
$scope.addCustomer = function ()
MyFactory.getClients(123456, function(clients) {
var curCust = clients[0];
$scope.status = clients.last_name;
})
}
}