2015-12-17 92 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