I have getNewsDetail() function that subscribes to an http get request. Then it stores in detailNews attribute what the function returns. The code works fine detailNews.created[0].value
returns its value, however, in console I get ERROR TypeError: ctx.detailNews.created is undefined
.
news.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { Observable } from 'rxjs';
import { INewsDetail } from '../../interfaces/news-detail';
@Injectable({
providedIn: 'root'
})
export class NewsService {
constructor(private http: HttpClient) { }
getNewsDetail(id):Observable<INewsDetail> {
return this.http.get(`${environment.url}`+`/node/`+id+`?_format=json`);
}
}
news-detail.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NewsService } from 'src/app/core/services/news/news.service';
@Component({
selector: 'app-news-detail',
templateUrl: './news-detail.component.html',
styleUrls: [ './news-detail.component.scss']
})
export class NewsDetailComponent implements OnInit {
detailNewsID: '';
detailNews: any = {};
constructor(
private newsService: NewsService,
private activatedRoute: ActivatedRoute
) { }
ngOnInit(): void {
this.activatedRoute.queryParams.subscribe(params => {
this.detailNewsID = params.id;
});
this.getNewsDetail();
}
getNewsDetail() {
this.newsService.getNewsDetail(this.detailNewsID).subscribe(news => {
this.detailNews = news;
});
}
}
news-detail.component.html
{{ detailNews }} // Renders: [object Object]
{{ detailNews.created[0].value }} // It renders the value, but console shows ERROR TypeError: ctx.detailNews.created is undefined.