ngModel 无法在表单标签内工作

ngModel not working inside form Tag

[(ngModel)] 在表单 Tag

中不起作用

当我使用 Multi Select Outside Form 标签对 select All 和 Deselect 所有函数

但是当我将 Form 放入其中时,它正在工作 Selecting 所有值

<form [formGroup]="roleForm">

    <mat-form-field class="example-full-width">
        <mat-select placeholder="Select Role Type" formControlName="roleType" (selectionChange)="roleTypeSelect($event.value)">
            <mat-option *ngFor="let role of roleTypeList" [value]="role.roleTypeId">
                {{role.roleTypeName}}
            </mat-option>
        </mat-select>
    </mat-form-field> <!-- Multi Select Mat Start -->
    <mat-form-field class="example-full-width">

        <mat-select placeholder="Select Privileges" class="filter-select" [formControl]="selectedItems" [compareWith]="equals" multiple
         #privilegeSelect="ngModel">
            <mat-option disabled="disabled" class="filter-option">
                <button mat-raised-button class="mat-primary fill text-sm" (click)="selectAll(privilegeSelect, dropdownList)">
                    Select All
                </button>
                <button mat-raised-button class="mat-primary fill text-sm eta-margin-all" (click)="deselectAll(privilegeSelect)">
                    Deselect All
                </button>
            </mat-option>
            <mat-option *ngFor="let privilege of dropdownList" [value]="privilege">{{privilege.itemName}}</mat-option>
        </mat-select>

    </mat-form-field>
    <!-- Multi select mat end -->

</form>

删除 ngModel,因为您正在使用 formControlName

我已经在 stackblitz

上创建了一个演示

component.ts

roleForm: FormGroup;
constructor(private fb: FormBuilder) {
    this.roleForm = this.fb.group({
      toppings: [null]
    })
}

toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];

selectAll() {
    this.roleForm.get('toppings').patchValue(['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'])
}
deselectAll() {
    this.roleForm.get('toppings').patchValue([])
}

component.html

<form [formGroup]="roleForm">
    <mat-form-field class="example-full-width">

        <mat-select  placeholder="Select Privileges" class="filter-select" formControlName="toppings" multiple>
            <mat-option disabled="disabled" class="filter-option">
                <button mat-raised-button class="mat-primary fill text-sm" (click)="selectAll()">
                    Select All
                </button>
                <button mat-raised-button class="mat-primary fill text-sm eta-margin-all" (click)="deselectAll()">
                    Deselect All
                </button>
            </mat-option>
            <mat-option *ngFor="let privilege of toppingList" [value]="privilege">{{privilege}}</mat-option>
        </mat-select>

    </mat-form-field>

<p>Selected Value--{{roleForm.get('toppings').value}}</p>

</form>

如果您只使用 ngModel 而没有周围的 <form>,则不必为输入指定 name

<input ... [(ngModel)]="model.something"`>

但是当您在表单中使用它时,name 属性将成为必需的!

<form>
  <input ... name="something" [(ngModel)]="model.something"`>
</form>

它实际上在文档中:

When using the ngModel within tags, you'll also need to supply a name attribute so that the control can be registered with the parent form under that name.

如果你错过了它,它根本不会显示任何错误,它只是不起作用。