简体   繁体   English

未定义标识符“ ...”。 '__type'不包含这样的成员Angular FormArray

[英]Identifier '…' is not defined. '__type' does not contain such a member Angular FormArray

I'm using the reactive form to create multiple checkboxes with FormArray but when I use it in my template got this error: 我正在使用反应形式使用FormArray创建多个复选框,但是当我在模板中使用它时出现以下错误:

Identifier 'roleNames' is not defined. 未定义标识符“ roleNames”。 '__type' does not contain such a memberAngular '__type'不包含这样的成员

this is my htmlCode: 这是我的htmlCode:

<form [formGroup]="createUserFormGroup" (ngSubmit)="submit()">
    <label formArrayName="roleNames"
        *ngFor="let role of createUserFormGroup.controls.roleNames.controls; let i = index">
        <input type="checkbox" [formControlName]="i">
        {{role[i].displayName}}
    </label>
    <button>submit</button>
</form>

This is my typescript code: 这是我的打字稿代码:

constructor(
public _userService: UserServiceProxy,
private _ouServiceProxy: OuServiceProxy,
private managetableService: ManageTablesService<UserDto>,
private formBuilder: FormBuilder) 
{
this.createUserFormGroup = this.formBuilder.group({
  'roleNames': new FormArray([]),
});  }

    this._userService.getRoles().subscribe(result => {
  this.roles = result["result"].items;
  this.addRoleCheckBoxes();
  // this.setInitialRolesStatus();
});

  addRoleCheckBoxes() {
debugger;
this.roles.map((roleDto, index) => {
  const control = new FormControl(index === 0);
  (this.createUserFormGroup.controls.roleNames as FormArray).push(control);
});

your *ngFor should be like this 您的* ngFor应该是这样的

*ngFor="let role of createUserFormGroup.get('roleNames')['controls']; let i = index"

Not like this 不是这样

*ngFor="let role of createUserFormGroup.controls.roleNames.controls; let i = index"

And to use dynamic array 并使用动态数组

<div [formGroupName]="i">
   <input type="checkbox" [formControlName]="your control name"> // not the index of the arra
    {{role[i].displayName}}
</div>

工作代码

Try to Update your HTML as following: 尝试按以下方式更新HTML:

<form [formGroup]="createUserFormGroup" (ngSubmit)="submit()">
    <label [formGroup]="role"
        *ngFor="let role of createUserFormGroup.controls.roleNames.controls; let i = index">
        <input type="checkbox" [formControlName]="i">
        {{role[i].displayName}}
    </label>
    <button>submit</button>
</form>

I find my problem 我发现我的问题

I must change this code with roles 我必须使用角色更改此代码

{{roles[i].displayName}}

no {{role[i].displayName}} 否{{role [i] .displayName}}

in fact, in formArray we only have the count of formControls no values of checkbox and we have to add value from another place 实际上,在formArray中,我们只有formControls的数量,而没有复选框的值,我们必须从另一个地方添加值

暂无
暂无

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

相关问题 未定义标识符“ X”。 “ Y”不包含此类成员Angular - Identifier 'X' is not defined. 'Y' does not contain such a member Angular 未定义角度标识符“ playerType”。 &#39;&#39;不包含这样的成员 - Angular Identifier 'playerType' is not defined. '' does not contain such a member 未定义标识符“标题”。 &#39;{}&#39; 不包含这样的成员 angular 8 - Identifier 'title' is not defined. '{}' does not contain such a member angular 8 已解决 Angular 标识符“标题”未定义。 'Movie[]' 不包含这样的成员 - Solved Angular Identifier 'title' is not defined. 'Movie[]' does not contain such a member 未定义标识符“必需”。 &#39;__type&#39; 不包含这样的成员 - Identifier 'required' is not defined. '__type' does not contain such a member Angular:标识符“标题”未定义。 'never' 不包含这样的成员。 异常问题 - Angular: Identifier 'title' is not defined. 'never' does not contain such a member. Unusual problem 未定义标识符“长度”。 &#39;null&#39; 不包含这样的成员 ng(0) - Identifier 'length' is not defined. 'null' does not contain such a member ng(0) 未定义标识符“名称”。 “对象”不包含这样的成员 - Identifier 'name' is not defined. 'object' does not contain such a member 标识符“图像”未定义。 'never' 不包含这样的成员 - Identifier 'image' is not defined. 'never' does not contain such a member 未定义标识符“X”。 'Y' 不包含这样的成员 - Identifier 'X' is not defined. 'Y' does not contain such a member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM