【发布时间】:2018-04-02 23:20:45
【问题描述】:
在使用少数可用的 Angular 5 Typescript 文档,了解如何使用 Observable 和 HttpClient 实现 CRUD 方法的新方法时,我想出了下一个代码,但仍然坚持显示来自 Web API 的数据:
这是我的模型:
export interface Value {
id: number;
name: string;
}
我的服务:
export class ValueService {
baseUrl = environment.apiUrl;
constructor(private authHttp: HttpClient) {}
getValues() {
return this.authHttp
.get<Value[]>(this.baseUrl + 'values', { observe: 'response'});
// here my baseUrl is 'http://example.com/api/'
}
}
我的组件:
@Component({
selector: 'app-values',
templateUrl: './values.component.html',
styleUrls: ['./values.component.css']
})
export class ValuesComponent implements OnInit {
values: Value[];
constructor(private valueService: ValueService, private route: ActivatedRoute) { }
ngOnInit() {
this.route.data.subscribe(data => {
this.values = data['values'].result;
});
}
}
最后,我的模板:
<ul>
<li>
<div *ngFor="let value of values | async"> {{value.name}}</div>
</li>
</ul>
我的数据显示在带有 HTTP 200 结果的浏览器控制台中,但不在我的页面上,我缺少什么才能让它在我的页面上呈现?
【问题讨论】:
标签: typescript httpclient observable angular5