我已經注入了storageService,但我試圖在鏈接函數中使用this.storageService訪問它的給定undefined。無法在角度打字稿中的指令中訪問自定義工廠
任何人都可以幫助我嗎?
module App.Directive {
import Services = Core.Services;
import Shared = Core.Shared;
export class UserNav implements ng.IDirective {
restrict = 'A';
require: any = "^?ngModel";
scope = true;
templateUrl: any = "template/user-nav.html";
link: ng.IDirectiveLinkFn = function(scope, element, attributes, ngModel: any) {
var a = this.storageService.getItem("userInfo", false);
console.log("getting > " + a);
}
constructor(public routerService: Services.RouterService,
public storageService: Services.StorageService) {
}
static factory(): any {
var directive = (routerService: Services.RouterService,
storageService: Services.StorageService) => {
return new UserNav(routerService, storageService);
}
directive['$inject'] = ['routerService', 'storageService'];
return directive;
}
}
}
有一個[方式如何指令](http://stackoverflow.com/q/33322282/1679310)可以創建一個示例和幾個鏈接 –