I have a search-results component composed by a list of items and a map. On the map component I have markers, and I want that when the user clicks one of the markers, the corresponding element in the list components gets animated.
Therefore, I have an @output and an event emitter on the map component that emits the id of the item clicked to the parent, and the parent sends this id to its child "list" component in order to to do its stuff. This input is a primitive type, a number.
The problem is that even though the property is changing in the parent when the event takes place, the input doesnt change in the child:
search-results.component.html
<div class="container">
<div class="left-side">
<app-search-addreses-form></app-search-addreses-form>
<app-results-list [travelToAnimate]="travelToAnimate" [travels$]="travels$"></app-results-list>
</div>
<div class="right-side">
<app-results-map
(propagateTravelId)="handleClickedMarker($event)"
[userOrigin]="userOrigin"
[userDestination]="userDestination"
[travels$]="travels$"
></app-results-map>
</div>
</div>
search-results.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Travel } from 'src/app/core/interfaces/travel';
import { TravelsService } from 'src/app/core/services/travels.service';
import { ActivatedRoute } from '@angular/router';
import { GeoPosition } from 'src/app/core/interfaces/travel-payload';
@Component({
selector: 'app-search-results',
templateUrl: './search-results.component.html',
styleUrls: ['./search-results.component.scss'],
})
export class SearchResultsComponent implements OnInit {
travels$: Observable<Travel[]>;
userOrigin: GeoPosition;
userDestination: GeoPosition;
travelToAnimate: number;
constructor(private travelsService: TravelsService, private route: ActivatedRoute) {}
ngOnInit(): void {
this.route.queryParams.subscribe((params) => {
this.userOrigin = {
address: '',
latitude: params.origin_latitude,
longitude: params.origin_longitude,
};
this.userDestination = {
address: '',
latitude: params.destination_latitude,
longitude: params.destination_longitude,
};
this.travels$ = this.travelsService.getTravelsNearOfDestination(
params.destination_latitude,
params.destination_longitude
);
});
}
handleClickedMarker(id: number): void {
this.travelToAnimate = id;
console.log('doing click', this.travelToAnimate); // "doing click 421" it changes when a marker is clicked
}
}
import { Component, Input, OnChanges } from '@angular/core';
import { Observable } from 'rxjs';
import { Travel } from 'src/app/core/interfaces/travel';
@Component({
selector: 'app-results-list',
templateUrl: './results-list.component.html',
styleUrls: ['./results-list.component.scss'],
})
export class ResultsListComponent implements OnChanges {
@Input() travels$: Observable<Travel[]>;
@Input() travelToAnimate: number;
constructor() {}
ngOnChanges(): void {
console.log('change detected: ', this.travelToAnimate); // "change detected :undefined", fired only once
}
}