2
私は2つの配列:categories
とarticles
を持っています。 category.name
によって置き換えられるするには、次のニュースフィード、categoryId
ニーズにAngular2:オブジェクトの配列から一意のIDでオブジェクトを取得する適切な方法ですか?
categories = [{id: 1, name: 'Gadgets'}, {id: 2, name: 'Artificial Intelligence'}, {id: 3, name: 'Opinions'}];
articles = [{id: 1, categoryId: 3, title: 'Title 1', body: 'Body Text 1'}, {id: 2, categoryId: 1, title: 'Title 2', body: 'Body Text 2'}, {id: 3, categoryId: 1, title: 'Title 3', body: 'Body Text 3'}];
:ここ
は、配列がどのように見えるかです。ビューから配列categories
をループすることなく、名前プロパティにアクセスできるように、配列から必要なオブジェクトを取得するにはどうすればよいですか?
テンプレートに{{category(article.categoryId).name}}
のようなことをしたいのですが、正しい構文ではありません。あなたは、カテゴリ名
ArticlesComponent
articles: Article[];
categories: Category[];
constructor(
private _articleService: ArticleService,
private _categoryService: CategoryService,
) {}
ngOnInit()
{
this._articleService.getArticles().subscribe(articles => {this.articles = articles});
this._categoryService.getCategories().subscribe(categories => {this.categories = categories});
}
私の解決策はあなたの場合に働いていますか?ヴィーブク –