【发布时间】:2018-02-28 17:48:50
【问题描述】:
我正在为我的 Angular 组件提供一个属性。属性是指一个切换样式的布尔属性,您将其放入 HTML 中而不指定属性的值:
相对于输入(这不是我想要的):
如果存在compact,则组件应将其值强制为布尔值:
-
<my-component compact="compact">// -
<my-component compact="true>// -
<my-component compact="false">//
如果compact 没有值,则映射为true:
-
<my-component compact>//
如果compact 丢失:
-
<my-component>//
但是,当我在没有属性值的情况下保留它时,组件会看到 null,因此即使我在构造函数中提供了默认值,我也无法区分 case #4 和 #5。
constructor(
@Attribute('compact') public compact,
) {
console.log(this.compact) // `null` for <my-component compact>
}
我应该怎么做?单独的属性装饰器是否有能力
【问题讨论】: