Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular component, check if attribute exists

Tags:

angular

Say I have an angular component ie <this-thing></this-thing> which does it's thing.

Now I want to add something extra to it, ie <this-thing glows></this-thing> I would like the component to change a little if the glow attribute is there.

I have managed to do it via <this-thing [glows]="true"></this-thing> and in the component I have

@Input() public set glows(value: boolean) {
    console.log(value);
}

But that becomes too verbose.

If glows attribute is not on the component, then it's false and if it is, then true.

Is there a better way to acheive this?

Thanks for looking.

like image 822
Sprep Avatar asked Sep 15 '25 05:09

Sprep


1 Answers

use @Attribute and @optional in constructor see the docs

constructor(@Optional() @Attribute('glow') glow: any) {
  console.log(glow)
  console.log(glow==undefined)
}

<hello glow></hello> //give you '',false
<hello></hello> //give you null,true
like image 191
Eliseo Avatar answered Sep 17 '25 19:09

Eliseo