Is there any way to cast inputs to a component to a number automatically?
For example this component initialised like so:
<list-of-playlists limit="5"></list-of-playlists>
And defined here:
@Component({
selector: 'list-of-playlists',
templateUrl: 'list-of-playlists.html'
})
export class MyPlaylistComponent implements OnInit {
@Input() limit: number;
ngOnInit() {
console.log(typeof limit); // string
}
}
Is there a way to cast limit
to a number or integer automatically?
Otherwise I would have to type is as a string or any in Typescript
, or to start the component by casting it to a number in ngOnInit()
, which I would like to avoid.
You need to use Property Binding: