【发布时间】:2021-01-25 13:24:47
【问题描述】:
在谷歌搜索了很多相关的答案和尝试之后,我似乎不得不在这里寻求帮助。 我有一个 Angular 应用程序,它的组件之一名为 stock-subscribe,用于显示客户订阅的费用类型。在这个组件中,我创建了另一个组件 stock-addsubs,用于显示该客户尚未订阅的费用类型。 enter image description here
显然,两个 feeType 列表可以组成一个完整的列表。从 stock-subscribe 中,我可以得到一个数组 subscribedFeeIds,它保存了那些订阅的 feeTypes 的所有 id。
我的要求是将这个数组 subscribedFeeIds 传递给 stock-addsubs 组件,以便我可以根据数组的这些 id 过滤掉那些尚未订阅的费用类型。
据我所知,从一个组件传递到其子组件的数据应该是一个简单的过程,两个组件的 html 模板都不应该涉及我的案例。在许多谷歌搜索的解决方案中,使用 @Output 和 @Input 似乎比事件发射更简单。但是,没有一个可以成功传递子组件中的数组元素。
我可以从 stock-subscribe 组件获取预期的 id 列表 (subscribedFeeIds[]),只要传递给子组件的数组不是 EMPTY,所有其余代码都可以正常工作。
1) stock-subscribe.component.ts
@Component({
selector: 'app-stock-subscribe',
templateUrl: './stock-subscribe.component.html',
styleUrls: ['./stock-subscribe.component.scss']
})
export class StockSubscribeComponent implements OnInit {
userSubscribe: ISubscribe;
@Output() subscribedFeeIds: any = [];
listData: MatTableDataSource<any>;
constructor(private accountService: AccountService,
private stockService: StockService) { }
ngOnInit(): void {
this.createSubsList();
}
createSubsList() {
this.accountService.getUserAccount()
.subscribe(account => {
let userId = account.id.toString();
this.stockService.getUserSubscribes(userId).subscribe((data: ISubscribe[]) => {
// get the id of subscribed fee type and thenpublish to other component
for (var i = 0; i < data.length; i++)
{
if (data[i].status)
this.subscribedFeeIds.push(data[i].fees[0].id);
}
console.log(this.subscribedFeeIds);
// prepare the list source for display
this.listData = new MatTableDataSource(data);
this.listData.sort = this.sort;
}, error => {
console.log(error);
});
}, error => {
console.log(error);
});
}
}
2) stock-addsubs.component.ts
@Component({
selector: 'app-stock-addsubs',
templateUrl: './stock-addsubs.component.html',
styleUrls: ['./stock-addsubs.component.scss']
})
export class StockAddsubsComponent implements OnInit {
listData: MatTableDataSource<any>;
@Input() subscribedFeeIds: any [];
constructor(private feesService: FeesService) { }
ngOnInit(): void {
this.createFeeList();
}
createFeeList() {
this.feesService.getFeeList().subscribe((data: IFee[]) => {
// filter those already subscribed
for (var i = 0; i < this.subscribedFeeIds.length; i++)
{
for (var j = 0; j < data.length; j++)
{
data = data.filter(f => f.id != this.subscribedFeeIds[i]);
}
}
// prepare data to display
this.listData = new MatTableDataSource(data);
}, error => {
console.log(error);
});
}
}
【问题讨论】:
-
您能否也显示相应的 HTML 代码,将
subscribedFeeIds传递给您的子组件?当你说neither of two component html templates should be involved for my case时,我不太明白。您必须在 HTML 中进行一些属性绑定才能将数组从父组件传递到子组件。 -
这些要传递的数组的id不会在HTML中使用,而只是作为过滤器来获取那些尚未订阅的feeTypes。所以这些数组的元素只在 ts 中消耗。这就是为什么我说“我的案例不应该涉及两个组件 html 模板”。很抱歉让您感到困惑,请忽略它。这就是我没有在这里发布 HTML 的原因。感谢您的时间和在这里的任何输入,谢谢先生。
-
但正如@Tarun 所说,您需要在两个组件之间建立连接。所以要么导入它,要么在 HTML 中使用它并隐式导入它。
-
感谢塔伦和美因茨。这是我的 HTML。
-
感谢塔伦和美因茨。这是我的 HTML。您可能会注意到之前发布的 ts 中没有 columnToDisplay 的属性,为了干净的目的而被删除。
标签: arrays angular input components output