1

The following code

let isBrowserFactory2=function(@Inject(PLATFORM_ID) platformId: string){ return isPlatformBrowser(platformId);}

Gets the following error:

Decorators are not valid here

And this one

let isBrowserFactory=(@Inject(PLATFORM_ID) platformId: string):boolean=> isPlatformBrowser(platformId)

Gets the Expression expected error.

  • Why I can't use typescript's parameter decorators for a function?
  • How can I use @Inject() to inject something into a factory method?

1 Answer 1

1

Use @Inject in the constructor.

isBrowserFactory: boolean;

constructor(@Inject(PLATFORM_ID) platformId string) { 
  this.isBrowserFactory = this.isPlatformBrowser(platformId);
}

This way you are declaring platformId injecting PLATFORM_ID. And then, you can call your method isPlatformBrowser that I belive returns a boolean.

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

6 Comments

Basically correct, but class methods are supported, not just constructors. Object literal concise methods are not supported thouigh
@AluanHaddad I thought you had to use decorators before class declaration or inside the constructor.
@AluanHaddad Oh! Thanks! :D
Glad to help. I updated my comment slightly for clarity. If you edit your answer, I would like to vote for it.
@AluanHaddad Thanks! You helped indeed. I think that this way could be clearer (updated answer). I understood what the OP was trying to do and I believe this way he may achieve it. Thanks for your help! :D
|

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.