I try to build a basic page.
In HTML
Title: {{postAbout.title}}
In TS. This works fine if object attribute is clearly defined
private postAbout = {
id: '',
title: ''
};
Use http service to load data
ngOnInit() {
this.getResult('postURL');
}
getResult(url) {
this.mapleRestData.load(url, { id: 27 }).subscribe(
data => { this.postAbout = data; console.log("About Page Title:" + this.postAbout.title) }
);
}
However I like to use generic object like this. It generates error
// private postAbout = {
// id: '',
// title: ''
// };
private postAbout: Object;
browser_adapter.ts:73 EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/about/about.html:32:63
ORIGINAL EXCEPTION: TypeError: Cannot read property 'title' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'title' of undefined
I even tried to create Post Class like
export class Post {
id: number;
title: String;
}
Then define it in TS
private postAbout: Post;
It still gave me same error.