第一个
在这里我应该怎么做才能访问属性id或name?当我搜索解决方案时,我看到了一些与将对象转换为数组相关的内容,然后使用嵌套的 *ngFor。但我不知道如何做到这一点。
完整的代码,如果你需要它,这里是为什么我需要先推到一个数组,然后到其他:
export class AboutComponent {
array_all: Array<{ page: any }> = [];
array_page: Array<{ id: number, name: string, link: string, image_url: string, image_alt: string }> = [];
constructor(){
let start_at: number = 0;
let last_slice: number = 0;
for(let i: number = start_at; i < this.knowledge_items.length; i++){
if(i%2 === 0 && i%3 === 0 && i !== last_slice){
this.array_all.push({page: this.array_page});
this.array_page = [];
this.array_page.push({
id: this.knowledge_items[i].id,
name: this.knowledge_items[i].name,
link: this.knowledge_items[i].link,
image_url: this.knowledge_items[i].image_url,
image_alt: this.knowledge_items[i].image_alt
});
start_at = i;
last_slice = i;
}
else{
this.array_page.push({
id: this.knowledge_items[i].id,
name: this.knowledge_items[i].name,
link: this.knowledge_items[i].link,
image_url: this.knowledge_items[i].image_url,
image_alt: this.knowledge_items[i].image_alt
});
if(i === this.knowledge_items.length - 1){
this.array_all.push({page: this.array_page});
this.array_page = [];
}
}
}
console.log(this.array_all);
}
knowledge_items: Array<{id: number, name: string, link: string, image_url: string, image_alt: string }> = [
{
id: 1,
name: 'C++',
link: 'https://es.wikipedia.org/wiki/C%2B%2B',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
image_alt: 'C++ programming language'
},
{
id: 2,
name: 'Python',
link: 'https://es.wikipedia.org/wiki/Python',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
image_alt: 'Python programming language'
},
{
id: 3,
name: 'C++',
link: 'https://es.wikipedia.org/wiki/C%2B%2B',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
image_alt: 'C++ programming language'
},
{
id: 4,
name: 'Python',
link: 'https://es.wikipedia.org/wiki/Python',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
image_alt: 'Python programming language'
},
{
id: 5,
name: 'C++',
link: 'https://es.wikipedia.org/wiki/C%2B%2B',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
image_alt: 'C++ programming language'
},
{
id: 6,
name: 'Python',
link: 'https://es.wikipedia.org/wiki/Python',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
image_alt: 'Python programming language'
},
{
id: 7,
name: 'C++',
link: 'https://es.wikipedia.org/wiki/C%2B%2B',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
image_alt: 'C++ programming language'
},
{
id: 8,
name: 'Python',
link: 'https://es.wikipedia.org/wiki/Python',
image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
image_alt: 'Python programming language'
}
}
2条答案
按热度按时间zqdjd7g91#
您编写了错误的HTML绑定来显示页面ID。您的页面ID在
array_all.page
属性中。有关详细信息,请查看console.log()。以下是您的问题的解决方案。
ni65a41a2#
您代码是错误的。
check this link
ts文件:
}
HTML档案: