angular - Angular如何将数组传递给组件
问题描述
如何将数组从模板传递到组件文件。我有一个if
取决于循环索引的条件。请帮助我找到正确的方法。像这样的东西
https://jsfiddle.net/HB7LU/7638
<ul>
<li *ngFor="let data of list_value | keyvalue; let i=index;" (click)="getListValue(i,data)">
<li>
<ul>
<li *ngFor="let item of data.value" *ngIf="data[i].active">
<li>
<ul>
<ul>
example.componet.ts 文件
export class SomeComponent{
list_value: Array<any> = [];
active: boolean;
getListValue(index: String, data: any) {
console.log("Test",data[index]);
list_value[index].active = !list_value[index].active
}
}
解决方案
将数据传递给组件的一种方法是@input
像这样使用角度:
<ul>
<li *ngFor="let data of list_value | keyvalue; let i=index;" (click)="getListValue(i,data)">
<li>
<ul>
<li *ngFor="let item of data.value" *ngIf="data[i].active">
<app-selector *ngSwitchCase="'observations'" [inputdata]=item></app-selector>
<li>
并在您的component.ts
文件中加载*ngFor
:
import { Component, OnInit, Input, NgModule, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-selector',
templateUrl: './selector.component.html',
styleUrls: ['./selector.component.scss']
})
export class ObservationsComponent implements OnChanges{
@Input() inputdata: any;
}
ngOnChanges() {
console.log(this.inputdata)
}
推荐阅读
- reactjs - FAILURE:构建失败并出现异常。* 出了什么问题:在项目 ':app' 中找不到任务 'installDebug'
- ios - SwiftUI 中的辅助功能配音焦点问题
- html - 如何根据 jquery/ajax 的其他输入字段中的选择值激活输入所需的输入?
- python-3.x - ValueError:无法强制转换为 Series,长度必须为 1:给定 503
- css - 如何解决引导行列空白区域问题?
- python - 为什么我收到异常 ModuleNotFoundError
- c - C.Heapsort .计数交换和比较的次数
- go - Go 中的原子操作是否确保其他变量对其他线程可见?
- gatsby - 如何在 Gatsby-plugin-image 中的 Gatsby 中将图像路径作为道具传递
- c - 从 C 中的文本中获取一行的函数