使用 formControlName 和 ngModel 的 Angular 6 警告

标签 angular angular-ngmodel angular-reactive-forms

我最近将 Angular 版本升级到了 6-rc。我收到以下警告

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

具体是什么意思?该链接没有任何 #use-with-ngmodel

片段

我想我需要删除 ngModel 并使用 formGroup 作为我的数据绑定(bind)对象。

最佳答案

现在您可以在此处找到文档:

https://angular.io/api/forms/FormControlName#use-with-ngmodel-is-deprecated

所以你有 3 个选择:

  1. 使用响应式表单

  2. 使用模板驱动的表单

  3. 静音警告(不推荐)

<!-- language: lang-ts -->

    imports: [
      ReactiveFormsModule.withConfig({warnOnNgModelWithFormControl: 'never'});
    ]

关于使用 formControlName 和 ngModel 的 Angular 6 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49918503/

相关文章:

Angular 4/5 - 动态更改基本 href

javascript - ng-model - 空输入返回 null

angular - 如何使用 Angular 5 中的数据绑定(bind)设置日期输入字段的值?

Angular 5 react 形式 - 禁用单选组中的一个单选选项

javascript - 属性 Angular 2 的绑定(bind)不会更新具有绑定(bind) ID 的关联输入

node.js - 在使用 Angular2 时,我们需要 Node.js 和 Express.js 吗?

angular - 在 Angular 4 Reactive Forms 中提交时显示验证消息

javascript - 如何防止用户不在输入字段中输入字母?

angular - 没有点的德语日期本地化?

javascript - AngularJS 的 ng-model 不绑定(bind)下拉菜单中的选定值(ng-options),也无法重新加载页面