简体   繁体   English

如何从Angular 2(模板驱动器)的Form.value中排除表单控件值

[英]How to exclude a Form Control value from Form.value in Angular 2 (Template Driven From)

I have a working template driven Angular 2 form. 我有一个工作模板驱动的Angular 2表单。 One of the fields uses an external module (ng2-bootstrap typeahead). 字段之一使用外部模块(提前ng2-bootstrap)。 [(ngModel)] two way binding points to an object in my component. [(ngModel)]通过两种方式绑定指向组件中的对象。

If I include name="companyName" attribute in that control, on submit I get the companyName: value under form.value object, which I don't want to, because I use ngModel bound property I've set in component. 如果我在该控件中包括name="companyName"属性,则在提交时,我不需要在form.value对象下获得companyName: value ,因为我使用的是在组件中设置的ngModel绑定属性。 I can't remove name attribute from that control, if I do, I get an error stating that I need to use: [ngModelOptions]="{standalone: true}" 我无法从该控件中删除名称属性,如果这样做,则会收到一条错误消息,指出我需要使用: [ngModelOptions]="{standalone: true}"

So I did that, but then when I make any change in the value, form's pristine property doesn't get affected since any model connection is cut off by above statement. 因此,我这样做了,但是当我对值进行任何更改时,由于上述语句切断了任何模型连接,因此窗体的原始属性不会受到影响。 I've tried to set form's pristine property on custom control's update event, doesn't work since pristine property of form is read only. 我试图在自定义控件的update事件上设置表单的原始属性,但由于表单的原始属性是只读的,因此无法正常工作。

Is there a way for template driven Angular2 form control's to be defined in such a way that they affect form's validity, but their value is not included in form.value collection? 有没有一种方法可以定义模板驱动的Angular2表单控件,以使其影响表单的有效性,但其值不包含在form.value集合中?

I've just did delete formData['companyName']; 我刚刚delete formData['companyName']; first thing before saving my form data. 保存表单数据之前的第一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM