[英]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.