から発射しました。 ホーム私は都市を選択し、選択された都市に基づいて、私のホールコンポーネントからすべてのホールをロードしようとしています。angular2子は私が親コンポーネント<strong><em>ホーム</em></strong>と子コンポーネント<strong><em>ホール</em></strong>を持つ親コンポーネント
ホームHTML:
<div class="dropdown">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Select your city</button>
<div class="dropdown-content">
<ul *ngFor = "let city of cities">
<a (click)="selectCity(city)" > {{city.name}} </a>
</ul>
</div>
<hall *ngIf = "isSelectHall" [city]="specific_city" ></hall>
</div>
ホームコンポーネント:
constructor(private homeService: HomeService) {
}
ngOnInit() {
this.homeService.getCities().then (
data => {
this.cities = data;
});
}
selectCity(city:any) {
this.specific_city = city;
this.isSelectHall = true;
}
ホールコンポーネント:
export class Hall implements OnInit{
@Input() city: any;
halls: Object;
openDiv : boolean;
specific_hall: Object;
cart : any;
cartCount: number;
cartHalls: any;
countHall: number;
constructor(private hallService: HallService){
this.cart=[];
}
ngOnInit(){
this.fetchData(this.city.id)
}
fetchData(cityId : any) {
this.hallService.fetchData(cityId)
.then(
data => {
this.halls = data;
});
}
clicked(item : any) {
this.specific_hall = item
}
onNotify(cartItems : any) : void {
this.cartHalls = cartItems;
this.cartCount = this.cartHalls.length;
}
}
ホールHTML:
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--4-col">
<div class="populate-table" [hidden]="!halls">
<table>
<tr>
<th>Name</th>
<th>Landmarks</th>
<th>Seating capacity</th>
<th>Details</th>
</tr>
<tr *ngFor="let item of halls">
<td> {{ item.name }} </td>
<td> {{ item.landmarks }} </td>
<td> {{ item.seating_capacity }} </td>
<td> <a (click)="clicked(item)"> See </a> </td>
</tr>
</table>
</div>
</div>
私が直面している問題は:最初にselectCity(city)
をクリックしてにホームを選択した都市に固有のすべてのホールをロードしますが、後で他の都市をクリックしても効果はありません。私はここで間違って何をしていますか?
Scrrenshot:
これは完全に機能しました。どうもありがとう。それが正しい方法であるかどうか、あるいは他の可能性があるかどうか、この親子コミュニケーションを実装した方法を教えてください。 – Nitish
8つの方法があります。リンクを確認してください。 – deek