typescript Angular 从子组件中的父组件获取阵列.ts

wfsdck30  于 2023-03-09  发布在  TypeScript
关注(0)|答案(1)|浏览(159)

我对Angular非常陌生,正在为几个概念而挣扎。我有一个子组件:

<ng-template #days>
    <div *ngFor="let day of mappedDays">
        <job-day-listing [jobDay]="day" [canEdit]="canEdit" (vehicleSelectChange)="vehicleSelectChanged($event)" (employeeSelectChange)="employeeSelectChanged($event)" (click)="clearSelection()"></job-day-listing>
    </div>
</ng-template>

我需要访问孩子的 typescript 中的“mappedDays”,但我似乎做不到。
我试着在孩子中创建一个输入变量:

@Input() mappedDays: JobDayMapping[];
mappedDays: JobDayMapping[];

在html中:

<ng-template #days>
    <div *ngFor="let day of mappedDays">
        <span>[mappedDays]="mappedDays"</span>
        <job-day-listing [jobDay]="day" [canEdit]="canEdit" (vehicleSelectChange)="vehicleSelectChanged($event)" (employeeSelectChange)="employeeSelectChanged($event)" (click)="clearSelection()"></job-day-listing>
    </div>
</ng-template>

然后,我尝试在数组上调用forEach,但在mappedDays数组上收到一个“undefined”错误:

this.mappedDays.forEach(md => {
  md.addEditButtonSelected = false;
});

我需要服务吗?

epggiuax

epggiuax1#

这种方法很好(通过@Input()绑定将mappedDays传递给子进程),但是代码中存在一些错误:
您的当前代码

<ng-template #days>
  <div *ngFor="let day of mappedDays">
    <span>[mappedDays]="mappedDays"</span>
    <job-day-listing
      [jobDay]="day"
      [canEdit]="canEdit"
      (vehicleSelectChange)="vehicleSelectChanged($event)"
      (employeeSelectChange)="employeeSelectChanged($event)"
      (click)="clearSelection()">
    </job-day-listing>
  </div>
</ng-template>
  • 不确定为什么需要<span>标记
  • [mappedDays]属性在span的开始标记(>)的错误一侧,因此它将在span中作为文本列出。

以下方法应该有效:
x一个一个一个一个x一个一个二个x

相关问题