gpt4 book ai didi

Angular4 : select [selected] not work for the first time

转载 作者:行者123 更新时间:2023-12-03 23:13:31 27 4
gpt4 key购买 nike

有一个代码:

<select name="department" class="form-control select" [(ngModel)]="departments" formControlName="departmentControl">    
<option *ngFor="let department of departments" [ngValue]="department" [selected]="department.id == this.departmentid">
{{ department.name }}
</option>
</select>

和功能:
isSelected(department): boolean {
debugger;
return department.id == this.departmentid;
}

部门 - 是用户详细信息组件的嵌套组件。第一次选择用户详细信息组件后,未选择部门。但是第二次一切正常。哪里出错了?

最佳答案

不需要:[selected]="department.id == this.departmentid",因为您正在使用 [(ngModel)]
[(ngModel)]="departments" 更改为 [(ngModel)]="departmentid"
[ngValue]="department" 更改为 [ngValue]="department.id"
最后它应该是这样的:

<select name="department" class="form-control select" [(ngModel)]="departmentid" formControlName="departmentControl">    
<option *ngFor="let department of departments" [ngValue]="department.id" >
{{ department.name }}
</option>
</select>

WORKING DEMO

关于Angular4 : select [selected] not work for the first time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48295308/

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