Я пытаюсь получить родительский и дочерний массив для отображения и отобразить его на ngFor . Я сталкиваюсь с проблемой, что дети не будут показывать во время ngFor
У меня есть служба, которая будет принимать данные с сервера. Эти данные примерно такие.
"teaser" : [ {
"id" : "1234567",
"headline" : "Developer",
"text" : "Like Developer you will need ...",
"image" : {
"altText" : "Code",
"srcSet" : [ {
"query" : "min-with: 960px",
"path" : "randomPhoto.jpg"
}, {
"query" : "min-with: 320px",
"path" : "randomPhoto.jpg"
} ]
},
"link" : {
"text" : "more",
"titel" : "Job Description",
"href" : "http://google.de",
"target" : ""
},
"category" : [ "Job" ]
},
До сих пор я могу получить только teaser.text
, но его дочерние элементы не отображаются в HTML, например: photo (path) и href with text.
Это мой код
Service
@Injectable({
providedIn: 'root'
})
export class TeaserService {
private baseUrl = "Here is the link which is getting the Json file";
constructor(private http: HttpClient) {
}
getTeaserList(): Observable<any> {
return this.http.get(`${this.baseUrl}`)
}
}
Вот модель.
export class Teaser {
id: number;
headline: string;
text: string;
image: string;
link: string;
category: string;
}
Вот файл TS
.
export class AppComponent implements OnInit {
public activeElement = 2;
teasers: Observable<Teaser>;
selectedTab = "Jobs";
constructor(private teaserService: TeaserService) {}
ngOnInit() {
this.reloadData();
}
reloadData() {
this.stages = this.stageService.getStageList();
this.teaserService.getTeaserList().subscribe(getTeasers => {
this.teasers = getTeasers.teaser;
});
}
Вот HTML
.
div class="row" >
<div class="col-md-3 col-md-offset-2" *ngFor="let teaser of teasers;">
<img alt="" class="w-100" src="../assets/images/teaser-01.jpg"/>
<p>{{teaser.text}}</p>
<a class="btn-more" href="#" >
<i class="fa fa-angle-right"></i> More</a>
</div>
</div>