【发布时间】:2019-01-10 18:21:03
【问题描述】:
我在我的站点中为添加类别创建了一个编辑表单。
我需要在进入编辑页面时为下拉菜单选择类别值。
1 - 我将请求发送到服务器以获取详细信息:
public GetCatDetail(id:number){
this.catService.GetCatDetail(id).subscribe((data)=>{
this.cat=data,
this.editCatFG.setValue({
pFaName:[data.pFaName],
pEnName:[data.pEnName],
subCat:[data.subCat]
}),
this.selectedCat=data.subCat
})
}
2- 向服务器发送请求以获取类别列表以填充下拉列表。
public GetAllCat(){
this.catService.GetAllCatList().subscribe((data)=>{
this.cats=data
})
}
3- 我用 html 填充下拉列表:
<select style="margin-right: 105px;" name="roleLevel" formControlName="subCat" [(ngModel)]="selectedCat">
<option selected> دسته اصلی </option>
<option *ngFor="let cat of cats" selectedCat="cat.id" [value]="cat.id" >{{cat.pFaName}}</option>
</select>
我现在需要为数据库中的类别选择存在的值并在下拉列表中选择该值。
我该怎么做?
【问题讨论】:
-
this.selectedCat,是不是和options中使用的cat.id同类型?
-
@Aragorn 是的。他们都是
number -
第一个错误是你不需要同时使用 formcontrol 和 ngModel。请使用其中任何一种。
-
对于列表中的选择下拉菜单,我建议您查看并尝试此模块。这对你很有帮助,也很容易。 ngx-select-dropdown
-
@SachinShah 你能帮我吗?
标签: javascript angular typescript angular6