I know I am not the first to ask about this, but I can't find an answer in the previous questions. I have this in one component
<div class="col-sm-5">
<laps
[lapsData]="rawLapsData"
[selectedTps]="selectedTps"
(lapsHandler)="lapsHandler($event)">
</laps>
</div>
<map
[lapsData]="rawLapsData"
class="col-sm-7">
</map>
In the controller rawLapsdata
gets mutated from time to time.
In laps
, the data is output as HTML
in a tabular format. This changes whenever rawLapsdata
changes.
My map
component needs to use ngOnChanges
as a trigger to redraw markers on a Google Map. The problem is that ngOnChanges
does not fire when rawLapsData
changes in the parent. What can I do?
import {Component, Input, OnInit, OnChanges, SimpleChange} from 'angular2/core';
@Component({
selector: 'map',
templateUrl: './components/edMap/edMap.html',
styleUrls: ['./components/edMap/edMap.css']
})
export class MapCmp implements OnInit, OnChanges {
@Input() lapsData: any;
map: google.maps.Map;
ngOnInit() {
...
}
ngOnChanges(changes: { [propName: string]: SimpleChange }) {
console.log('ngOnChanges = ', changes['lapsData']);
if (this.map) this.drawMarkers();
}
Update: ngOnChanges
is not working, but it looks as though lapsData
is being updated. In the ngOnInit
is an event listener for zoom changes that also calls this.drawmarkers
. When I change the zoom I do indeed see a change in markers. So the only issue is that I don't get the notification at the time the input data changes.
In the parent, I have this line. (Recall that the change is reflected in laps, but not in map
).
this.rawLapsData = deletePoints(this.rawLapsData, this.selectedTps);
And note that this.rawLapsData
is itself a pointer to the middle of a large json object
this.rawLapsData = this.main.data.TrainingCenterDatabase.Activities[0].Activity[0].Lap;
zone.run(...)
should do it then. – DeliusngOnChanges()
will not be called. You can usengDoCheck()
and implement your own logic to determine if the array contents have changed.lapsData
is updated because it has/is a reference to the same array asrawLapsData
. – ZipahrawLapsData
, one option you have is to create a new array rather than modify the array contents. ThenngOnChanges()
will kick in. – ZipahrawLapsData
and that triggersngOnChanges
. I know you explained it, but I still don't see how laps can pick up on the change (surely it must be using something equivalent to ngOnChanges itself?) while map can't. – AubartadeletePoints
returns a new object. – Fabrin