>我创建了一个具有双向数据绑定的组件
class MyCustom2Way{
@Input() text: string;
@Output() textChange = new EventEmitter<string>();
// MyCustom2Way has something in the template that will
// trigger testChange.emit when user interacts with it
}
现在假设我以这样的形式使用 MyCustom2Way
:
<form action="" #myForm="ngForm">
<my-custom-2-way [(text)]="model.field" name="field"></my-custom-2-way>
</form>
当用户使用MyCustom2Way
迭代时MyCustom2Way
如何myForm
变脏?
你应该使用ngModel
和自定义ControlValueAccessor
,否则form对你的组件一无所知,所以它不会被标记为脏。 [(text)]="model.field"
- 只是句法糖。
您可以将表单传递给我的自定义 2 路组件,如下所示
<my-custom-2-way [(text)]="model.field" name="field" [form]="myForm"></my-custom-2-way>
然后在我的自定义组件中
class MyCustom2Way{
@Input() text: string;
@Output() textChange = new EventEmitter<string>();
// MyCustom2Way has something in the template that will
// trigger testChange.emit when user interacts with it
@Input Form:any
this.form._pristine=true;
}