javascript - 我们可以将 Angular FormArray 与 angular2-multiselect 下拉菜单一起使用吗?

标签 javascript html angular typescript angular-reactive-forms

对于我的用例,我必须构建一个表单,以便我们可以动态地将行添加到表单列中。我使用 Angular FormArray 构建了它。在该表单中,我必须添加一个下拉列表,用户可以在其中选择任务并可以从任务列表中搜索指定的任务。所以我尝试使用 angular2-multiselect 下拉菜单。 当我添加新行时,前一行下拉列表选定的值将在新添加的行中更新。谁能帮我解决这些问题吗?

这里我包含了带有错误的示例代码

StackBlitz link

form.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, FormArray } from '@angular/forms';


@Component({
  selector: 'app-reactive-form',
  templateUrl: './reactive-form.component.html',
  styleUrls: ['./reactive-form.component.css']
})
export class ReactiveFormComponent implements OnInit {

  tasksDropDownSettings = {};
  selectedTasks = [];

  constructor(private fb: FormBuilder) { }

  todoForm : FormGroup;
  tasksData = [
    {id: 1, taskName: "Go for Jogging"},
    {id: 2, taskName: "Complete the work"},
    {id: 3, taskName: "Catch the bus"},
    {id: 4, taskName: "Leave by 6"}
  ]

  ngOnInit() {
    this.todoForm = this.fb.group({
      tasks: this.fb.array([this.fb.group({task:''})])
    })

    this.tasksDropDownSettings = {
      singleSelection: true,
      enableSearchFilter: true,
      text: "Select Primary Skill",
      showCheckbox: true,
      labelKey: 'skillName',
      selectAllText:'Select All',
      unSelectAllText:'UnSelect All'
    }


  }

  get tasks() {
    return this.todoForm.get('tasks') as FormArray;
  }

  addTask() {
    this.tasks.push(this.fb.group({task:''}));
  }

* form.component.html *

<form [formGroup] = "todoForm">

  <label> Add tasks here </label> &nbsp;

  <div formArrayName = "tasks">
[enter image description here][1]
    <div *ngFor="let item of tasks.controls; let taskIndex=index" [formGroupName]="taskIndex" (click) = "onClick(tasks.controls)">

      <angular2-multiselect [data] = "tasksData" 
        [(ngModel)] = "selectedTasks" 
        [settings] = "tasksDropDownSettings"
        formControlName="task">

      </angular2-multiselect>
    </div>

  </div>

  <button type = "submit" class = "btn btn-submit"> Save </button>


  <button (click) = "addTask()"> Add Row </button>

</form>


<pre>{{ this.todoForm.value | json }}</pre>

最佳答案

由于您使用的是响应式(Reactive)表单,因此必须删除 [(ngModel)]

  <angular2-multiselect [data] = "tasksData" 
    [settings] = "tasksDropDownSettings"
    formControlName="task">
  </angular2-multiselect>

Working example

关于javascript - 我们可以将 Angular FormArray 与 angular2-multiselect 下拉菜单一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59385720/

相关文章:

javascript - 将类添加到一个元素而不是所有元素,例如里面

javascript - 无法删除 [object Array] 的属性 '1'

javascript - AngularJS:如何修复范围覆盖?

javascript - "Error: Arguments array must have arguments."应用模块

javascript - 使用 Redux Reducer 的 Immunity Helper 迭代数组并更改特定属性

javascript - 什么阻止了复选框的检查?

html - 应用于 anchor 标记的相同类 IE 11 生成两个不同的背景图像

javascript - 使用 react-router 中的 MemoryRouter 使用 javascript 导航

angular - ng 测试失败,Angular6

angular - 无法绑定(bind)到 'opened',因为它不是 'md-sidenav' 的已知属性