我使用模板表单构建了一个包含大量输入的大表单。现在我需要动态添加一部分输入。由于使用 Reactive Form 动态添加输入似乎更容易,我想将输入的特定部分更改为 Reactive Form。
那么是否可以在同一个表单标签中混合使用响应式表单和模板表单?
最佳答案
您可以混合使用 reactive forms和模板驱动的表单,但强烈不推荐。这是因为使用 ngModel
react 形式违背了形式状态不变性的想法。
响应式表单的原则遵循“单向”数据绑定(bind)规则,即您遵循管理表单状态的不可变方法,以便在模板和组件逻辑之间有更大的关注点分离。您可以在第一段的链接上阅读更多关于响应式(Reactive)表单的优点。
假设您正在继续混合模板驱动的表单和响应式(Reactive)表单。运行 ng serve
时控制台会抛出以下错误:
It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7 For more information on this, see our API docs here: https://angular.io/api/forms/FormControlName#use-with-ngmodel
关于angular - 混合 react 形式和模板形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55739509/