我有一篇文章類定義,像這樣:角2打字稿:域()不是一個函數
export class Article {
id : number;
isanon : boolean;
title: string;
link: string;
text: string;
subverse : string;
userID : string;
votes: number;
constructor(title: string, link: string, subverse : string, text : string, userID : string, votes?: number) {
this.title = title;
this.link = link;
this.text = text;
this.subverse = subverse;
this.userID = userID;
this.votes = votes || 0;
}
log() : void{
console.log("Title: " + this.title + " Link: " + this.link + " subverse: " + this.subverse);
}
domain(): string {
try {
const link: string = this.link.split('//')[1];
return link.split('/')[0];
} catch (err) {
return null;
}
}
voteUp(): void {
this.votes += 1;
}
voteDown(): void {
this.votes -= 1;
}
}
,我用觀察到的服務
export class HomeComponent implements OnInit {
articles : Article[];
從數據庫中獲取物品和...
this.service.GetArticles(this.subverseStr).subscribe((data)=>{
this.articles = <Article[]>data;
});
但是,在我的HTML模板中,它不能識別domain()或任何其他TS函數。
<div class="meta">({{ article.domain() }})</div>
當加載頁面時,我得到錯誤:
core.umd.js:2837 EXCEPTION: Error in app/article/article.component.html:15:20 caused by: self.context.article.domain is not a function
它並認識到它作爲一個功能,如果我硬代碼我的文章[],它並認識到成員變量。任何想法是怎麼回事?謝謝。
請提供'GetArticles'和其他相關部分的代碼。根本不明顯,'data'是'Article'實例的數組,'{{article ...'引用'Article'實例並不明顯。 [MCVE](http://stackoverflow.com/help/mcve)是必要的。 – estus
GetArticles和其他代碼可以在這裏找到:https://github.com/claysmith/hackerspulse/blob/master/wwwroot/app/services/app.service.hackerspulse.ts –
GetArticles返回解析的JSON。這不是'文章'。並且不能有'域名'方法。 – estus