3

i have injected storageService but i tried to access in the link function using this.storageService its giving undefined.

Can any one help me on this ?

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;
    }
  }
}
muenchdo
  • 2,161
  • 1
  • 17
  • 30
  • There is a [way how directive](http://stackoverflow.com/q/33322282/1679310) could be created with an example and few links – Radim Köhler Dec 17 '15 at 13:10

1 Answers1

0

So the problem is that the link function Change:

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

To:

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: The above answer won't work. Because the link function is used as a callback and this is the global object. You'd have to set the services as a variable inside your module, then you'd have access to it through scope. see my fiddle

Raulucco
  • 3,406
  • 1
  • 21
  • 27