在Angular2中投入数字?
问题描述:
有什么方法可以自动将组件的输入转换为数字吗?在Angular2中投入数字?
例如该组件初始化像这样:
<list-of-playlists limit="5"></list-of-playlists>
这里定义:
@Component({
selector: 'list-of-playlists',
templateUrl: 'list-of-playlists.html'
})
export class MyPlaylistComponent implements OnInit {
@Input() limit: number;
ngOnInit() {
console.log(typeof limit); // string
}
}
有没有办法投limit
自动数字或整数?
不然我必须键入是如Typescript
字符串或任何,或通过将其流延在ngOnInit()
一个数,这是我想避免启动组件。
答
根据http://victorsavkin.com/post/119943127151/angular-2-template-syntax(部穿入常量)
<list-of-playlists limit="5">
等同于(即语法糖)
<list-of-playlists [limit]=" '5' ">
那是,它总是导致字符串绑定。 由于@Sasxa在他的回答中已经提到,使用属性绑定来获得所需的类型。
+0
谢谢你的阐述。这是你提供的一个很好的链接。 – skovmand
辉煌。谢谢。 – skovmand