我有一个从服务器获取的部门列表的动态数组。我想在初始化时将该数组推入数组,基本上我想根据部门名称或数组中的ID显示复选框。我知道如何以反应形式推送空数组,但是如何使用现有数组初始化。实际上是一个更新/编辑组件
departmentList:any=[]; //array contains all departments
SelectedDeptList:any=[]; //fetched from db , selected departments
userForm: FormGroup;
this.userForm = this.fb.group({ //fb form builder
'phone': [null],
'departmentList': this.fb.array([this.createDepartment()]),
})
createDepartment(): FormGroup {
return this.fb.group({
'name': ''//checkbox value true or false
});
}
Run Code Online (Sandbox Code Playgroud)
模板
<div formArrayName="departmentList"
*ngFor="let item of
userForm.get('departmentList').controls; let i = index;">
<div class="col-md-6" [formGroupName]="i">
<div class="form-group">
<div class="col-md-4">
<label class="mt-checkbox mt-checkbox-outline">
<input formControlName="name" type="checkbox" > Department Name
<span></span>
</label>
</div>
</div>
</div></div>
Run Code Online (Sandbox Code Playgroud)
去做 ?
1)我怎么能填充或初始化所有的dept复选框的列表,那些应该是真实的存在或存在于我的“ SelectedDeptList”数组中(从数据库中获取)。
在此先感谢,任何建议将不胜感激。
我有一个包含3个字段的部分,当点击新按钮时,我必须得到相同的部分.我没有错误,所以我没有得到我错的地方
HTML:
<form *ngFor="let emergency of Emergencies; let i = index">
<div>
<label > Name</label>
<input type="text" [(ngModel)]="emergency.Name" name="emergency.Name"
/>
</div>
<div>
<label >Phone</label>
<input type="text" OnlyNumber="true" [(ngModel)]="emergency.Phone" name="emergency.Phone"
maxlength="15" />
</div>
<div>
<label >Relations</label>
<select class="col-lg-6 col-md-6 col-sm-6 col-xs-6"[(ngModel)]="emergency.ship">
<option >{{ship.Description}}</option>
</select>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud) 我是Angular的新手,并尝试从以下示例扩展示例代码:Angular 4 Form FormArray添加一个按钮以添加或删除表单输入行
如果在下拉菜单中选择了某个选项,则该选项的新选项应可表示。我到此为止没有问题,但是为那些“较低层选项”实现formControlName标记使我出错:
ContentComponent.html:56错误错误:找不到路径为'inhalt-> 0-> optionsKey'(…)的控件
.html中的确切行是:(输入formControlName =“ optionsKey”)
我在其他线程(Angular 2形式“找不到带有路径的控件”)或(Angular 2反应形式:找不到带有路径的控件)或(错误:找不到带有路径的控件:'x'angular 2)中寻找了那些错误,但是它们的问题是基于没有FormBuilder的ReactiveForm-Components创建的。我使用Formbuilder创建了我的所有ReactiveForm-Components,您将在我的代码中看到它们。
我可能错过了某件事还是做错了方向?
content.html
<h3 class="page-header">Formular-Abschnitt hinzufügen</h3>
<button type="button" (click)="addNewFormControl()" class="btn btn-primary">Neues Formularelement</button><br>
<form [formGroup]="formContent">
<div>
<label>Bezeichnung des Abschnittes</label>
<input formControlName="absatz">
</div>
<div formArrayName="inhalt">
<!--
In the template we are iterating this formarray,
and that is just the path to the formarray, which is:
invoiceForm.controls.itemRows.controls
'invoiceForm' being the complete form object, 'controls' being the content of the form …
Run Code Online (Sandbox Code Playgroud)