Angular 2 Reactive Forms:无法找到带路径的控件

时间:2017-05-27 18:26:16

标签: angular typescript web frontend angular-reactive-forms

我尝试动态地将角色添加到我的用户/角色应用程序中。我有一个Formarray,我可以在编辑视图中显示用户的角色。并且有一个按钮可以向用户添加更多角色。但是当我按下按钮"添加角色"时,我收到了以下错误消息:

  

错误错误:找不到路径控件:' rolesArr - > 1 - >名称'

在此示例中,我尝试向要创建的用户添加多个角色。

这是我的代码:

users-edit.component.html (摘录)

<div formArrayName="rolesArr" >
<div class="row">
    <div class="col-md-10 col-md-offset-2">
        <button class="btn btn-default"
                type="button"
                (click)="addRole()">Add Role
        </button>
    </div>
</div>
<br>
<div *ngFor="let role of roles.controls; let i=index">
    <div [formGroupName]="i">

        <div class="form-group">
            <label class="col-md-2 control-label" [attr.for]="i">Role</label>

            <div class="col-md-8">
                <input class="form-control"
                        [id]="i" 
                        type="text" 
                        placeholder="Role" 
                        formControlName="name" />
            </div>
        </div>
    </div>
</div>

users-edit.component.ts (摘录)

addRole() {
    this.roles.push(this.fb.group(new Roles()));
}

ngOnInit(): void {

    this.userForm = this.fb.group({
        username: ['', [Validators.required, Validators.minLength(3)]],
        firstname: ['', [Validators.required, Validators.minLength(3)]],
        lastname: ['', [Validators.required, Validators.minLength(3)]],
        password: ['', [Validators.required, Validators.minLength(10)]],
        rolesArr: this.fb.array([])
    });

    this.sub = this.activeRoute.params.subscribe(
        params => {
            let id = +params['id']; //converts string'id' to a number
            this.getUser(id);
        }
    );
}
getUser(id: number) {
        this.userService.getUser(id).subscribe(
                user => this.onUserRetrieved(user)
            );
}
onUserRetrieved(user: User): void {
    console.log("OnUserRetrieved: " + user.firstname);
    if (this.userForm) {
        this.userForm.reset();
    }
    this.users = user;

    if (this.users.id === 0) {
        this.pageTitle = 'Add User';
    } else {
        this.pageTitle = `Edit User: ${this.users.username}`;
    }

    //Update the data on the form
    this.userForm.patchValue({
        username: this.users.username,
        firstname: this.users.firstname,
        lastname: this.users.lastname,
        password: this.users.password
    });

    const roleFGs = this.users.roles.map(roles => this.fb.group(roles));
    const roleFormArray = this.fb.array(roleFGs);
    this.userForm.setControl('rolesArr', roleFormArray);
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您应该创建一个包含Roles控件的表单组,如下所示

createRole() :  FormGroup {
    return this.fb.group({
            name: '',
            type: '',

    });
}

然后你应该推动角色如下,

addRole() {
    this.roles.push(this.createRole());
}

答案 1 :(得分:1)

当Aravind answer工作时,你仍然能够完成与现在完全相同的事情:

addRole() {
    this.roles.push(this.fb.group(new Roles()));
}

但控件需要类中属性的默认值(这正是Aravind的解决方案所做的,它会创建一个具有默认值的对象)

所以,如果你当前的Roles课程看起来像这样:

export class Roles {
    public name: string;
    public type: string;
}

你应该添加如下默认值:

export class Roles {
    public name: string = '';
    public type: string = '';
}

这允许angular找到您的属性作为控件

其他信息: https://angular.io/guide/reactive-forms#use-formarray-to-present-an-array-of-formgroups