0
我是Angular2的新手,我目前正在努力在ngFor循環中導入組件。Angular ts - 使用ngFor的本地數據導入組件
看來,當您在ngFor循環中導入包含數據的組件時,它會檢查.ts文件中的值,而不是ngFor中使用的局部變量。我想要做的是將ngFor中使用的變量發送到下一個組件。
accommodations.component(父視圖)
TS:
import { Component, Input } from '@angular/core';
import { AccommodationModel } from '../../../../models/reservations/accommodation-model';
@Component({
selector: 'reservations-reservation-accommodations',
templateUrl: './accommodations.component.html'
})
export class AccommodationsComponent {
constructor() { }
@Input() accommodations: AccommodationModel[];
}
HTML:
<div class="card-block">
<ngb-tabset>
<ngb-tab *ngFor="let accommodation of accommodations" title="{{accommodation.arrivalDate}}">
<ng-template ngbTabContent>
<div class="table-responsive">
<table class="table table-condensed table-bordered table-striped content-divider-up">
<thead>
<tr>
<th>ArrivalDate</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{accommodation.arrivalDate}}
</td>
</tr>
</tbody>
</table>
</div>
</ng-template>
</ngb-tab>
</ngb-tabset>
<reservations-reservation-accommodations-costs [costs]="accommodation.costs"></reservations-reservation-accommodations-costs>
</div>
costs.component(子視圖)
TS:
import { Component, Input } from '@angular/core';
import { CostModel } from '../../../../../models/reservations/cost-model';
@Component({
selector: 'reservations-reservation-accommodations-costs',
templateUrl: './costs.component.html'
})
export class CostsComponent {
constructor() { }
@Input() costs: CostModel[];
}
HTML:
<div class="row content-divider-up">
<div class="col">
{{costs.length}}
</div>
</div>
如何發送讓住宿(.costs)在ngFor到costs.component?通過上面的代碼,costs.component中的成本總是未定義,因爲它在我不想要的amenities.components.ts文件中查找名爲「accommodation」的變量。
哇真的嗎?我錯過了嗎?多麼尷尬哈哈,謝謝。編輯:嗯我不知道當局部變量和scrope外的名稱具有相同的名稱時會發生什麼。 – Reft