从父组件到子组件的 Angular 传递数组

标签 angular typescript angular-cli

在我们开始之前,我大约 1 周前开始学习 Angular,不要害怕直接回到基础。

那么你是怎么做到的呢? 在 app.component.ts 中,我有一个标准数组,需要由多个子组件访问。

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  masterArray = ['Create', 'Foo', 'Bar'];
}

如何将其拉入由 angular cli 生成的子组件。 例如我需要在导航组件中使用它来生成导航面板。

<li *ngFor = "let i of masterArray; let ind = index">
  <a>{{i}}</a>
</li>

最佳答案

您可以将数组作为子组件的输入。

@Component({
  selector: 'app-navigation',
  templateUrl: './navi.component.html',
  styleUrls: ['./navi.component.css']
})
export class NavigationComponent {
  @Input() masterArray : string[];
}

并将数组传给父组件html中的子组件。

<app-navigation [masterArray]="masterArray"></app-navigation>

关于从父组件到子组件的 Angular 传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171832/

相关文章:

javascript - 将类添加到 Angular2 指令中的下一个元素

Angular Material Design - 固定侧边栏

typescript - Angular2 - 私有(private)变量是否可以在模板中访问?

Angular 7 fatal error : Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

angular - vendor.bundle.js 中捆绑了什么?

angular - 在 Angular 库项目 package.json 文件中保持版本号同步

Angular 设计模式 : MVC, MVVM 还是 MV*?

html - 如何使表格行在 Angular 2 中充当超链接

TypeScript keyof 返回特定类型

jquery - Angular 4 缩放