要将ngFor指令的内容显示在多个页面而不是一个大列表中,你可以使用ngIf指令来控制在每个页面上显示的数据。
以下是一个示例代码,展示了如何使用ngFor和ngIf来实现在多个页面上显示数据:
在组件中定义一个数组来存储所有的数据,以及一个变量来存储当前页面的数据:
@Component({
selector: 'app-my-component',
template: `
{{ item.title }}
`
})
export class MyComponent {
allData = [
{ title: 'Page 1 Content', page: 1 },
{ title: 'Page 2 Content', page: 2 },
{ title: 'Page 1 Content 2', page: 1 },
{ title: 'Page 3 Content', page: 3 },
// ...
];
currentPage = 1;
}
在模板中,使用ngFor指令遍历所有的数据,并使用ngIf指令根据当前页面的值来过滤数据。只有当item的page属性等于currentPage时,才会在页面上显示该条数据。
你可以根据需要,在不同的页面上改变currentPage的值,从而显示不同的数据。