Javascript AngularJS:TypeError:无法读取属性';然后';未定义的

Javascript AngularJS:TypeError:无法读取属性';然后';未定义的,javascript,angularjs,promise,Javascript,Angularjs,Promise,首先,我是新来的。我准备了一些Web Api,我想从中获取一些数据。 我的服务功能获取日期(工作正常): 和控制器: var role = []; authService.getRole().then(function (results) { role = results.data;... 在控制器中的此函数中,我得到异常: TypeError: Cannot read property 'then' of undefined at n.$scope.login (http://lo

首先,我是新来的。我准备了一些Web Api,我想从中获取一些数据。 我的服务功能获取日期(工作正常):

和控制器:

var role = [];
authService.getRole().then(function (results) {
     role = results.data;...
在控制器中的此函数中,我得到异常:

TypeError: Cannot read property 'then' of undefined
at n.$scope.login (http://localhost/.../app/controllers/loginController.js:27:30)
at ib.functionCall (http://localhost/.../Scripts/angular.min.js:199:303)
at Ec.(anonymous function).compile.d.on.f (http://localhost/.../Scripts/angular.min.js:216:74)
at n.$get.n.$eval (http://localhost/.../Scripts/angular.min.js:126:15)
at n.$get.n.$apply (http://localhost/.../Scripts/angular.min.js:126:241)
at HTMLButtonElement.<anonymous> (http://localhost/.../Scripts/angular.min.js:216:126)
at HTMLButtonElement.c (http://localhost/.../Scripts/angular.min.js:32:389)
TypeError:无法读取未定义的属性“then”
位于n.$scope.login(http://localhost/.../app/controllers/loginController.js:27:30)
随时通话(http://localhost/.../Scripts/angular.min.js:199:303)
at Ec.(匿名函数).compile.d.on.f(http://localhost/.../Scripts/angular.min.js:216:74)
在n$get.n$eval(http://localhost/.../Scripts/angular.min.js:126:15)
在n.$get.n.$apply(http://localhost/.../Scripts/angular.min.js:126:241)
在HTMLButtoneElement。(http://localhost/.../Scripts/angular.min.js:216:126)
在HTMLButtonElement.c(http://localhost/.../Scripts/angular.min.js:32:389)

请帮忙。谢谢。

您需要从函数返回承诺,
$http
确实会返回自身

代码

var _getRole = function () {
    return $http.get(serviceBase + 'api/User/CurrentUserRoles');
};

您是否注入了
$http
?这是服务-当然,在controller-nook中,现在-您没有从
\u getRole
@c69返回承诺重复的答案告诉您创建自定义承诺并返回该自定义承诺..我不希望这样…您可以查看我的答案..这是通过
$http
重用承诺返回。然后(函数(结果){return results;})看起来完全多余?删除它。@mar14没问题。很高兴能帮助您,谢谢:)
var _getRole = function () {
    return $http.get(serviceBase + 'api/User/CurrentUserRoles');
};