我有 2 个 factory
函数:
工厂
factory.getCurrEmployee = function()
{
data = {"api_token": authenticationFactory.getToken()};
url = GLOBALS.url + 'show/employee/' + $cookieStore.get('employeeid');
return requestFactory.post(url, data)
.then(function (response) {
return response.data.result.Employee;
}, function () {
$window.location.assign('/');
});
}
factory.isSuperadministrator = function() {
factory.getCurrEmployee().then(function (employee) {
if(employee.Role == 'Superadministrator')
{
return true; //console.log('Superadministrator') <- that console.log is visible in my console
}
return false;
});
}
return factory;
在我的 Controller 中,我期望 true 或 false(用户是否是 super 管理员)但结果是什么。如果我在 factory.isSuperadministrator 中使用 console.log,结果为真。
Controller
console.log(employeeFactory.isSuperadministrator());
为什么这不起作用?
最佳答案
在 factory.isSuperadministrator
函数中未返回 factory.getCurrEmployee
的 promise 。我还进行了一些小的重构,使您的代码更智能。
factory.isSuperadministrator = function() {
return factory.getCurrEmployee().then(function (employee) {
return employee.Role == 'Superadministrator';
});
}
但是上面的东西并没有解决你的问题,它只会在console
中print
promise对象。要进一步解决您的问题,您需要将 .then
函数与 employeeFactory.isSuperadministrator()
返回的 promise 一起放置,如下所示
Controller
employeeFactory.isSuperadministrator().then(function(data){
console.log(res);
});
请遵循此处描述的相同指南 this answer
关于javascript - AngularJs 空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203846/