在Angular中,可以使用@Input装饰器将数组名称传递给ngFor指令。下面是一个示例:
在父组件中,定义一个包含数组的属性,并使用@Input装饰器将其暴露给子组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
`
})
export class ParentComponent {
myArray = ['Item 1', 'Item 2', 'Item 3'];
}
然后,在子组件中,接收这个数组名称并将其传递给ngFor指令:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
{{ item }}
`
})
export class ChildComponent {
@Input() items: string[];
}
在上述示例中,父组件使用@Input装饰器将myArray属性传递给子组件的items属性。子组件使用ngFor指令遍历items数组并显示每个元素。
确保在父模块和子模块中都正确引入和声明这两个组件,以及在需要使用它们的模板中正确使用它们的选择器。