0
我試圖實現一個基於角色的訪問控制系統,其中允許的資源將在登錄後從服務器加載。我可以設法使用原始JavaScript代碼進行檢查。Angular TypeScript指令的鏈接方法不會被調用
angular.module('app').directive('accessControl',
[
'AuthService', function (authService) {
return {
restrict: 'A',
scope: "=",
link: function (scope, element, attrs) {
scope.canShow = function(resource) {
var allowedResources = authService.accountInfo.resources;
return allowedResources.indexOf(resource) !== -1;
}
}
}
}
]);
但因爲我的整個應用程序是打字稿,我一直在努力使純打字稿的指令,但不幸的是,我不能這樣做。這是我的TS代碼。
export class AccessControl implements ng.IDirective {
public authService: App.AuthService;
public link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => void;
constructor(authService: App.AuthService) {
this.authService = authService;
console.log('authservice: ', authService);
AccessControl.prototype.link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {
scope["canShow"] = function (resource: string) {
// some logic
console.log('can show' + resource);
return true;
};
};
}
public static factory(): ng.IDirectiveFactory {
var directive = (authService: App.AuthService) => {
return new AccessControl(authService);
};
directive['$inject'] = ['AuthService'];
return directive;
}
restrict = "A";
scope: "=";
}
angular.module('app').directive('accessControl', AccessControl.factory());
鏈接函數永遠不會被調用。 任何幫助或指針將不勝感激。