gpt4 book ai didi

angular - ngModel 无法在表单标签内工作

转载 作者:行者123 更新时间:2023-12-05 09:15:44 28 4
gpt4 key购买 nike

[(ngModel)] 在表单标签内不工作

当我使用 Multi Select Outside Form 标签时,可以正常选择 All 和取消选择所有功能

但是当我把它放入 Form 里面时,它正在工作 Selecting All Values

<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没有周围<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.

如果您错过了它,它根本不会显示任何错误,它只是无法工作。

关于angular - ngModel 无法在表单标签内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com