Angularjs 在指令中,我无法访问位于authService中的“getUser”方法
在指令中,我无法访问authService中的Angularjs 在指令中,我无法访问位于authService中的“getUser”方法,angularjs,Angularjs,在指令中,我无法访问authService中的getUser方法: angular.module('app').directive('guard', [ function ( authService) { return { restrict: 'A', link: function (scope, element, attrs) { // element.hide();
getUser
方法:
angular.module('app').directive('guard', [ function ( authService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
// element.hide();
function currentUserHasAnyPermission(requiredP) {
return authService.getUser().then(function (currentUser) {
var currentUserP = currentUser.permissions;
return _.intersection(currentUserP, requiredP).length > 0;
});
}
}
};
}]);
返回一个错误:
Cannot read property 'getUser' of undefined
at currentUserHasAnyPermission
移除方括号:
̶a̶n̶g̶u̶l̶a̶r̶.̶m̶o̶d̶u̶l̶e̶(̶'̶a̶p̶p̶'̶)̶.̶d̶i̶r̶e̶c̶t̶i̶v̶e̶(̶'̶g̶u̶a̶r̶d̶'̶,̶ ̶[̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶(̶ ̶a̶u̶t̶h̶S̶e̶r̶v̶i̶c̶e̶)̶ ̶{̶
angular.module('app').directive('guard', function ( authService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
// element.hide();
function currentUserHasAnyPermission(requiredP) {
return authService.getUser().then(function (currentUser) {
var currentUserP = currentUser.permissions;
return _.intersection(currentUserP, requiredP).length > 0;
});
}
}
};
̶}̶]̶)̶;̶
});
有关详细信息,请参见I deleted,但返回一个错误:
TypeError:authService.getUser(…)。then不是函数
Yea。进步然后,下一步是查看authService
工厂。你真的要求互联网调试你的程序吗?今天是学习如何自己调试东西的好日子。请参见okey,如何正确调用currentUserHasAnyPermission
函数eturns错误:currentUserHasAnyPermission(…)。then不是此行元素.hide()中的函数
也给了我一个错误:element.hide不是函数
为什么我不能调用函数element.hide()代码>在该指令中?