I've a class as shown below
export class Story {
id: number;
title: string;
storyText: string;
utcDate: string;
get displayDate(): string {
const today = new Date();
const postDate = new Date(this.utcDate);
const td = today.getTime();
const pd = postDate.getTime();
return ((td-pd)/1000)+'ms';
}
}
HTML view is shown below
<span>
<i class="fa fa-clock-o" aria-hidden="true"></i>
{{story.displayDate}}
</span>
Here, the html code block is inside and ngFor loop as *ngFor="let story of stories" and stories is an array of type Story , stories: Story[];
But its not displaying anything . No error also. What am I doing wrong here? Will this work like this without an explicit setter property? Or should I create a setter property and set the value manually?
edit: below my script that populate the story array and the service function
loadData() {
this.storyService.stories()
.subscribe(
data => {
const response = <Story[]>data.message;
this.stories = response;
},
error => {
alert('error');
});
}
stories() {
return this.http.get<Result>(this.baseUrl + '/story/list/', this.httpOptions);
}
public story = new Story ;in your app.component.tsnew Story(). Or better, a complete minimal example reproducing the problem. If you don't havenew Story()anywhere, then you're never constructing any Story instance.new Storyfor an accessor to work.