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;
    }
  }
}
1
  • There is a way how directive could be created with an example and few links Commented Dec 17, 2015 at 13:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Its not working. Its show the following error TypeError: Cannot read property 'getItem' of undefined
@KiranGopal That's right, I updated the answer and added a link to a fiddle with my proposal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.