Reputation: 2016
I have a parent component with the following code in the parent.component.html:
<app-child [idElement]=(idElement)></app-child>
In the child component, I have the input parameter like this:
@Input() idElement : number;
And also a function called
getSpecs()
I want the getSpecs()
function to be executed by the child when the input is modified by the parent. Is that possible?
Upvotes: 5
Views: 12125
Reputation: 101
There is another option without OnChanges
.
@Component({
selector: 'app-child',
template: '<div></div>'
})
export class AppChildComponent {
private _idElement: number;
get idElement(): number {
return this._idElement;
}
@Input('idElement') set idElement(value: number) {
if (value) {
this._idElement = value;
this.getSpecs();
}
}
}
Hope this helps! :)
Upvotes: 9
Reputation: 1943
You can try these one
Import OnChanges
from angular core package in your child component
import { Component, Input, OnChanges , SimpleChanges} from '@angular/core';
Implement you child class like
export class YourComponent implements OnChanges
Create OnChanges
method like
ngOnChanges(changes:SimpleChanges){
console.log(changes.your input property name);
// implement your logic here
}
Whenever you have changes in parent component it will affect in child component
Upvotes: 1