代码之家  ›  专栏  ›  技术社区  ›  harkesh kumar

ngModel不在表单标记内工作

  •  2
  • harkesh kumar  · 技术社区  · 8 年前

    [(ngModel)]

    取消选择所有功能

    但是当我把它放在表格里时,我选择了所有的值

    <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>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Krishna Rathore    8 年前

    删除 ngModel 因为你在用 formControlName .

    我已经创建了一个演示 stackblitz

    组件.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([])
    }
    

    组件.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>
    
    推荐文章