2015-12-17 62 views
3

我已經注入了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; 
    } 
    } 
} 
+0

有一個[方式如何指令](http://stackoverflow.com/q/33322282/1679310)可以創建一個示例和幾個鏈接 –

回答

0

所以問題是,鏈接功能 變化:

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; 
    } 
    } 
} 

要:

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(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; 
    } 
    } 
} 

UPDATE:以上答案都不行。因爲鏈接功能用作回調,並且this是全局對象。您必須將服務設置爲模塊內的變量,然後才能通過範圍訪問它。 see my fiddle

+0

它不工作。它顯示以下錯誤TypeError:無法讀取未定義的屬性'getItem' –

+1

@KiranGopal這是正確的,我更新了答案並添加了一個鏈接到我的提案小提琴 – Raulucco