2

I want to use @Input value to initialize other variable in angular 4

export class test {
  @Input() id: string;

  condition: boolean = (this.id == 'myid') //Getting this.id as undefined
}

2 Answers 2

2

You can use id variable directly like id instead of this.id inside a class itself. You have to access variable using this only when variable appeared inside function. But eventually this wouldn't help you, since id value will always be undefined.

Ideally you should use this value inside ngOnInit hook which gets fired when component gets initialized and you will see the value passed from the consumer component.

export class test {
  @Input() id: string;
  condition: boolean;

  ngOnInit(){
     this.condition = (this.id == 'myid')
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your @Input is first available at ngOnInit. So you can't access it before ngOnInit call.

Comments

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.