加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

角度为2 rc5的基于模型的形式不起作用

发布时间:2020-12-17 17:35:39 所属栏目:安全 来源:网络整理
导读:我正在将代码从角度beta迁移到RC5版本. 我面临着使用的基于模型的表格的问题. 因为我已经在角度2 beta中开发了很多形式.我很难将基于模型的表单更改为基于模板的表单. 任何形式迁移的帮助都非常感谢. 我现有的代码是这样的 profile.ts import {FORM_DIRECTIV
我正在将代码从角度beta迁移到RC5版本.
我面临着使用的基于模型的表格的问题.
因为我已经在角度2 beta中开发了很多形式.我很难将基于模型的表单更改为基于模板的表单.
任何形式迁移的帮助都非常感谢.

我现有的代码是这样的

profile.ts

import {FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES} from '@angular/forms';
import {FormBuilder,FormGroup,FormControl,Validators} from '@angular/forms';

 this.firstName = new FormControl();
        this.lastName = new FormControl();
        this.email = new FormControl();
        this.addressLine = new FormControl();
        this.postal = new FormControl();
        this.postalArea = new FormControl();

        this.form = builder.group({
            firstName: this.firstName,lastName: this.lastName,email: this.email,addressLine: this.addressLine,postal: this.postal,postalArea: this.postalArea,photo: this.photo
        });

和profile中的模板.HTML如下

<form class="form-default"  [formGroup]="form">

<input type="text" class="form-control" id="firstName" [(ngModel)]="model.firstName" formControlName="firstName" maxlength="200">

    <input type="text" class="form-control" id="lastName" [(ngModel)]="model.lastName" formControlName="lastName" maxlength="200">
    <input type="text" class="form-control" id="email" [(ngModel)]="model.username" readonly formControlName="email" maxlength="100">

    </form>

我在控制台中遇到以下错误.

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in
/assets/scripts/my-profile/my-profile.html:176:66 ORIGINAL EXCEPTION:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using
formGroup’s partner directive “formControlName” instead. Example:

06002

解决方法

在这里您可以找到 answer to the same problem只需添加:

[ngModelOptions]="{standalone: true}"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读