【问题标题】:ngModel not working inside form TagngModel 在表单标签内不起作用
【发布时间】:2018-08-23 08:47:26
【问题描述】:

[(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>

【问题讨论】:

  • 感谢 Krishna Rathore

标签: angular angularjs-directive angular-material multi-select


【解决方案1】:

如果您只使用ngModel 而不使用周围的&lt;form&gt;,则不必为输入指定name

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

但是当你在表单中使用它时,name 属性就变成了强制性的!

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

它实际上在文档中:

在标签中使用 ngModel 时,您还需要提供一个 name 属性,以便控件可以使用该名称注册到父窗体。

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

【讨论】:

    【解决方案2】:

    删除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>
    

    【讨论】:

    • 我有 Json 的对象作为 List 所以我如何应用 PatchValue 请告诉我
    • {id: 1, itemName: "Login 1"} {id: 2, itemName: "Configuration"} {id: 3, itemName: "Organization"} {id: 4, itemName: "用户管理"} {id: 5, itemName: "通知管理"} {id: 6, itemName: "Functions"}
    • 是的,它工作得很好,我还有一个问题
    • 有没有材料设计中文件上传的demo
    • 我没有上传材料文件的演示。让我创建,我会更新你。
    猜你喜欢
    • 2017-06-17
    • 2017-10-21
    • 2019-08-27
    • 2021-09-11
    • 2018-12-04
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多