I'm having an object from an API where I want to display the dynamic values in the value field of a form in Angular.
TS2339: Property 'title' does not exist on type 'Object'.
HTML:
<h1>Edit News Post</h1>
<form>
<div class="form-group" >
<label for="newsTitle">Title</label>
<input id="newsTitle" type="text" class="form-control" value="{{ news.title }}">
</div>
</form>
component.ts:
export class NyheterEditComponent implements OnInit {
news: Object = [];
constructor(private http: HttpClient, private postsService: PostsService, private route: ActivatedRoute) {
}
ngOnInit(): void {
console.log(this.route.snapshot.paramMap.get('id'));
const id = Number(this.route.snapshot.paramMap.get('id'));
this.postsService.fetchNewsItem(id).subscribe(response => {
console.log(response);
this.news = response;
})
}
The object I'm trying to access is displaying in the console as:
{
id: 4,
title: 'Zebrapower',
description: 'Sebra som en Sebra',
image_path: 'https://images.unsplash.com/photo-1646206924799-c9…lfHx8fGVufDB8fHx8&auto=format&fit=crop&w=884&q=80',
active: true
}
I bet it's just a silly mistake, but I can't access it no matter what I try!
EDIT: Added fetchNewsItem from posts.service.ts
fetchNewsItem(id: number) {
const options = {headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
params: {
id: id,
},
};
return this.http.get('https://infoboard-api.setpoint.no/infoboard/news/news-by-id', options)
}
news, like{ id: number; title: string; description: string; image_path: string; active: boolean; }.