gpt4 book ai didi

Angular 组件,检查属性是否存在

转载 作者:行者123 更新时间:2023-12-01 23:59:36 25 4
gpt4 key购买 nike

假设我有一个 Angular 分量,即 <this-thing></this-thing>这是它的事情。

现在我想给它添加一些额外的东西,即<this-thing glows></this-thing>如果存在 glow 属性,我希望组件稍微改变一下。

我已经通过 <this-thing [glows]="true"></this-thing> 做到了在我的组件中

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

但这变得太冗长了。

如果 glows 属性不在组件上,则它为 false,如果是,则为 true。

有没有更好的方法来实现这一点?

感谢观看。

最佳答案

在构造函数中使用@Attribute@optional 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

关于 Angular 组件,检查属性是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62082021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com