0 day streak
javascript / intermediate
Snippet

Input Transforms for Booleans

The 'transform' property in @Input allows developers to sanitize incoming data automatically. Using 'booleanAttribute' ensures that presence-only attributes in HTML are correctly evaluated as true rather than empty strings.

snippet.js
javascript
1
@Input({ transform: booleanAttribute }) isRequired = false;
angular
Breakdown
1
@Input({ transform: booleanAttribute })
Applies a transformation function to the input value before it is assigned to the property.
2
isRequired = false;
The property now correctly handles boolean values passed from the parent template.