我必须将API json数据(嵌套对象响应)显示为ion-accordion。如何实现?下面是component.ts和service文件的代码
组件.ts代码:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
@Component({
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.scss']
})
export class ViewComponent implements OnInit {
users: any = [];
constructor(private userdata: DataService) { }
ngOnInit() {
this.userdata.get().subscribe(data =>{
this.users = [data];
console.log(this.users);
});
}
}
服务代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: HttpClient) { }
get():Observable<any>{
return this.http.get<any>(' ');
}
}
1条答案
按热度按时间xlpyo6sf1#
这将取决于“this.users”的格式。但是您可以使用 *ngFor来创建您的accordion。
例如,如果在TS文件中有用户数据:
在HTML文件中: