我需要从Angular的Controller方法中调用Factory方法。但是当我调用Factory方法AuthFactory.login(username,password).then()
时,它显示了这样的错误
TypeError: AuthFactory.login(...).then is not a function
Plunker code here。
我想我在这里缺少AngularJS Promise的概念。
最佳答案
对于登录方法,您正在发送简单的javascript object
而不是promise
对象。
function login(username, password) {
var userInfo = {
"authToken": "6a65dd0c-b35a-429b-a9c0-f5c327ec5d6f",
"id": "1445138519"
};
return userInfo; // Returning javascript object
}
在plunkr中,您有一些注释的代码,如下所示:
// $http.post(API_URL + '/auth/login', {
// email: username,
// password: password
// })
因此,代替返回
userInfo
,返回$http.post
对象,然后使用then
方法。