Jos*_*eph 2 json angular-http angular angular-httpclient
我需要帮助在Angular 4中显示来自api的subscribe输出.我怎么能这样做,因为我写了data.data.data,但它说属性数据不存在于类型对象上.我将如何在浏览器中输出?这是我下面的代码和下面的api图片
import { Component, OnInit } from '@angular/core';
import { NewsService } from '../news.service';
@Component({
selector: 'app-news-list',
templateUrl: './news-list.component.html',
styleUrls: ['./news-list.component.css']
})
export class NewsListComponent implements OnInit {
constructor(private newsService: NewsService) { }
ngOnInit() {
this.newsService.getNews()
.subscribe(
data => {
alert("News Success");
console.log(data);
},
error => {
alert("ERROR");
});
}
}
Run Code Online (Sandbox Code Playgroud)
在组件中创建属性
myData: any[] = [];
Run Code Online (Sandbox Code Playgroud)
并在您的订户功能
import { Component, OnInit } from '@angular/core';
import { NewsService } from '../news.service';
@Component({
selector: 'app-news-list',
templateUrl: './news-list.component.html',
styleUrls: ['./news-list.component.css']
})
export class NewsListComponent implements OnInit {
constructor(private newsService: NewsService) { }
ngOnInit() {
this.newsService.getNews()
.subscribe(
(res: any) => {
alert("News Success");
this.myData = res.data;
// Where you find the array res.data or res.data.data
console.log('res is ', res.data);
},
error => {
alert("ERROR");
});
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的模板中
1)查看JSON的选项
<pre>{{myData | json}}</pre>
Run Code Online (Sandbox Code Playgroud)
2)如果你有数组,循环选项
<div *ngFor="let d of myData">
{{d}}
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20892 次 |
| 最近记录: |