[英]how can I have custom two way databinding make a form dirty
I created a component with two way databinding 我创建了具有两种数据绑定方式的组件
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
}
Now suppose I use MyCustom2Way
in a form like this : 现在,假设我以这样的形式使用MyCustom2Way
:
<form action="" #myForm="ngForm">
<my-custom-2-way [(text)]="model.field" name="field"></my-custom-2-way>
</form>
How can I have MyCustom2Way
turn myForm
dirty when user iteracts with MyCustom2Way
? 当用户使用MyCustom2Way
迭代时,如何让MyCustom2Way
弄脏myForm
?
You should use ngModel
and custom ControlValueAccessor
, otherwise form does not know anything about your component, so it will not be marked as dirty. 您应该使用ngModel
和自定义ControlValueAccessor
,否则form对您的组件一无所知,因此不会被标记为脏。 [(text)]="model.field"
- is just syntactic sugar. [(text)]="model.field"
-只是语法糖。
You can pass the form to my-custom-2-way component like this 您可以像这样将表单传递给my-custom-2-way组件
<my-custom-2-way [(text)]="model.field" name="field" [form]="myForm"></my-custom-2-way>
then in my custom component 然后在我的自定义组件中
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.