Boolean Attributes in Custom Angular Components
I like clean code so being able to have boolean attributes like required in my custom Angular components makes me happy. To do it however you need an extra bit of javascript.
If you want to boolean attributes, a.k.a. @Input() properties on your custom Angular components, you need to add small amount of javascript to get it to work correctly.
By default, Angular will treat the value of wicked
in the example above as an empty string (""
). To allow the input to be used as a boolean attribute, you have to manually set it to true
if the value is an empty string in the ngOnInit
function of the custom component.
Note this has to be done in ngOnInit
instead of the constructor for value to be picked up correctly.