I've got a child component in my Angular 5 application that does two-way binding, and the binding is working properly. I'm having a bit of an issue with knowing about the change though in the parent controller. In the parent I use the child component like so:
<app-lab-hazard-classification-question-group
primaryQuestion="Does the lab contain mechanical tools?"
[(primaryValue)]="lab.hazardClassificationDetails.mechanicalExists"></app-lab-hazard-classification-question-group>
The lab
variable is an @Input()
to the parent control. When I evaluate the value of lab.hazardClassificationDetails.mechanicalExists
it's properly being set by the child component. I need to know though, in the parent control, when that value gets modified. Because the lab
variable itself doesn't change, the ngOnChanges
method isn't actually called.
Do I have to add a (change)
item to the child controller and explicitly bind that to be notified of the change? I was hoping for a more generic way as I have around 10 of these child elements, and I don't care "what" value it changed to, just that it did in fact change.
The child component's .ts file has this:
@Input() primaryValue: boolean;
@Output() primaryValueChange = new EventEmitter();
onPrimaryChanged(): void {
this.primaryValueChange.emit(this.primaryValue);
}
and the HTML has something like this:
<select class="form-control" id="primary" name="primary" required
[(ngModel)]="primaryValue"
(change)="onPrimaryChanged()">
<option [ngValue]="null || undefined">-- Select --</option>
<option [ngValue]="true">Yes</option>
<option [ngValue]="false">No</option>
</select>