【发布时间】:2018-05-04 02:39:24
【问题描述】:
我正在使用 Http 导入以 Angular 5 调用 api 端点来填充选择下拉列表,但是当我将它记录到控制台并且下拉列表中没有填充任何数据时,我变得不确定......它意味着是项目类别.
item-category.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import {Router} from '@angular/router';
import { Globals } from '../shared/api';
import { of } from 'rxjs/observable/of';
import 'rxjs/add/operator/toPromise';
declare var $: any;
@Injectable()
export class ItemCategoryService{
private categoryUrl = this.globals.CATEGORYS_URL;
constructor(private http: Http, private globals: Globals, private router:Router) { }
fetchCategories(){
let v = this.page_header();
return this.http.get(this.categoryURL, v)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
};
}
itemCategory.component.ts
fetchCategorys(){
this.categorySrv.fetchCategories().then(response =>this.categorys = response.results )
.catch(error=> this.error = error )
console.log(this.categorys); // <== undefined
}
itemCategory.component.html
<select class="form-control" [(ngModel)]="product.category"[formControl]="productForm.controls['productCategory']" require>
<option *ngFor="let item of categorys" [value]="item.slug">{{item.name}}</option>
</select>
这就是我所拥有的,但未定义的是我在控制台中得到的,下拉列表中没有任何来自 api 的内容,检查也没有显示任何内容......我可能出了什么问题?
【问题讨论】:
-
那是因为你已经在消费
fetchCategories()ofietm-category.ts中的Promise
标签: javascript angular angular5 angular-http