【发布时间】:2019-06-12 18:57:48
【问题描述】:
下面是使用 3 个变量并为每个变量分配默认值的输入装饰器
@Input() score: number = 0;
@Input() text: string = 'test';
@Input() color: string = 'red';
这就是我将值传递给 ngFor 内的组件的方式。
[text]="item.name"
[score]="item.score"
[color]="item.color"
如果我的 item 对象不包含 color 属性,那么组件中的 color 变量应该采用 'red' 作为默认值。
但是当我将其记录为:
ngOnInit() {
console.log(this.score, this.text, this.color);
}
那么颜色变量将undefined作为值。
这是上述日志的控制台
8 "English" undefined
6 "Spanish" "blue"
第一个日志是当项目 不 包含 color 属性时,第二个是当它包含属性 color 且值为 蓝色
【问题讨论】:
标签: javascript angular angular-input