typescript 有人能帮助我吗请,因为它不显示选项禁用[值]=“null”选定>选择用户类型< /option>

mbjcgjjk  于 2022-11-26  发布在  TypeScript
关注(0)|答案(2)|浏览(149)

enter image description here

<select class="form-control" id="ddSelectaTopic" onchange=
                                    "if(this.value==='') {this.style.color='#999'} else {this.style.color='#333'}" [(ngModel)]="user_type_id" (change)="TypeChange($event.target.value,access_id)"  [disabled]="access_id == 1 || !access_id">
                                        <option disabled [value]="null" selected>Choose User Types</option>
                                        <option *ngFor="let type of UserTypes; let i = index" [attr.value]="type.id" [attr.selected]="i == 0 ? true : null">
                                            <span *ngIf="access_id">{{type.name}}</span>
                                        </option>
                                        <option value="newType">New User Type</option>
                                    </select>

我尝试更改已禁用的值null selected,但未显示“选择用户类型”

6ie5vjzr

6ie5vjzr1#

如果你想将null值绑定到select选项,使用[ngValue]="null"。通过使用[value]="null",Angular将始终将值绑定为字符串。因此,当比较完成时,你会得到一个字符串值,与空值进行比较,空值始终为false。这可能是你的选项没有被选中的原因。

<option disabled [ngValue]="0" selected>Choose User Types</option>
<option *ngFor="let type of UserTypes; let i = index" [ngValue]="type.id">
  <span>{{type.name}}</span>
</option>
91zkwejq

91zkwejq2#

使用此value=""而不是[value]="null"来显示“选择用户类型”

<select class="form-control" id="ddSelectaTopic"
    onchange="if(this.value==='') {this.style.color='#999'} else {this.style.color='#333'}" [(ngModel)]="user_type_id"
    (change)="TypeChange($event.target.value,access_id)" [disabled]="access_id == 1 || !access_id">
    <option disabled value="" selected>Choose User Types</option>
    <option *ngFor="let type of UserTypes; let i = index" [attr.value]="type.id" [attr.selected]="i == 0 ? true : null">
        <span *ngIf="access_id">{{type.name}}</span>
    </option>
    <option value="newType">New User Type</option>
</select>

相关问题