我无法从 promise 对象的下拉列表中获取数据,它在控制台响应中显示数据。 Angular 承诺

I'm not able to get data in drop-down from promise object, it showing data in console response. Angular Promise

component.ts 文件如下:

deptList: Department[];
  ngOnInit() {

   this.service.getDeptList().then(res => this.deptList= 
    res as Department[]);
    this.resetForm();
 }

Service.ts 下面:

getDeptList(){
return this.http.get(environment.apiURL+'/dept/getAll').toPromise();

} html 文件如下:

 <div class="form-group">
        <label>Department</label>
        <select  name="department" #department="ngModel" [(ngModel)]="service.formData.department" 
               class="form-control">
        <option value="0">-Select-</option>
        <option *ngFor="let dept of deptList" value="{{dept.id}}">{{dept.name}}</option>    
        </select>
        </div>

现在,你的循环如下:

<tr *ngFor="let itemlist of service.requestItemList; let i=index">
</tr>
<td>{{itemlist.item}}</td>
<td>{{itemlist.code}}</td>
<td>{{itemlist.requestedQty}}</td>
<td>{{itemlist.rate}}</td>
<td>{{itemlist.dateRequiredBy}}</td>
<td> <a class="btn btn-sm btn-info text-white" (click)="AddOrEditOrderItem(null, service.formData.id)">
        <i class="fa fa-pencil"></i></a>
</td>

cannot read property item of undefined:这个错误与上面的代码有关,要解决你必须把你所有的td放在tr

里面

修改您的代码,如下所示:

<tr *ngFor="let itemlist of service.requestItemList; let i=index">
    <td>{{itemlist.item}}</td>
    <td>{{itemlist.code}}</td>
    <td>{{itemlist.requestedQty}}</td>
    <td>{{itemlist.rate}}</td>
    <td>{{itemlist.dateRequiredBy}}</td>
    <td> <a class="btn btn-sm btn-info text-white" (click)="AddOrEditOrderItem(null, service.formData.id)">
            <i class="fa fa-pencil"></i></a>
    </td>
</tr>