【发布时间】:2021-07-13 11:29:37
【问题描述】:
当我在 Angular 中动态使用两种方式绑定时,我遇到了意外的行为,
类似[(ngModel)]="condition ? propA : propB"。
更多详情,在我的app.component.ts:
@Component({ ... })
export class AppComponent {
props = { a: 'this is a', b: 'this is b' };
myCondition = 'a';
}
还有我的模板:
<h2>props a: {{ props.a }} (type anything, last input will change)</h2>
<input #a [(ngModel)]="props.a" />
<hr>
<h2>props b: {{ props.b }}</h2>
<input #b [(ngModel)]="props.b" />
<hr>
<h2>last one (type anything, nothing happen)</h2>
<input #c [(ngModel)]="myCondition === 'a' ? props.a : props.b" />
我认为 input#c 应该更新了props.a,但什么也没发生。但是,在 input#a 中输入一些内容会触发 input#c 更改:(
谁能解释我发生了什么事?谢谢。
【问题讨论】:
标签: angular