【发布时间】:2017-10-08 01:08:08
【问题描述】:
我正在尝试编写一个组件以在我的应用程序中重用,它有时会显示一个控制按钮,有时不会。
理想情况下,我希望从属性的存在或不存在中获取此信息,以便使用该组件看起来像<generic-component hideControls></generic-component>,在我的组件中根据该属性是否存在一个布尔变量,但是我看不出有办法做到这一点。
有没有办法做到这一点?
我已经尝试过下面稍微杂乱的版本(尽管理想情况下我不需要在 showControls/hideControls 上设置值);
generic.component.html
<div>
<div>Stuff</div>
<div *ngIf="showControls">Controls</div>
</div>
generic.component.ts
// imports etc.
@Component({
selector: 'generic-selector',
templateUrl: 'generic.component.html'
})
export class GenericComponent implements OnInit {
@Input()
public showControls: boolean = true;
// other inputs and logic
}
这失败了,因为用法 <generic-selector showControls="false"></generic-selector> 将 showControls 设置为字符串“false”,这是真实的,而不是布尔值。因此,我必须通过向组件添加大量混乱来解决它以接收输入并根据它是否被赋予字符串“false”来转换为布尔值。一种非混乱的排序方式将不胜感激,但我更希望能够执行上述其他选项。
【问题讨论】:
标签: angular