Suppose I have simple Angular2 component
@Component({ selector: 'parent' })
@View({
template: `
<p>Parent {{ data }}</p>
<child [model]="data"></child>
`,
directives : [Child]
})
export class Parent {
data: number = 42;
}
as you can see it uses another simple component
@Component({
selector: 'child',
properties : ['model']
})
@View({
template: `
<p>Child {{ model }}</p>
`,
})
export class Child {
model: number;
}
I am passing model
from the parent
component to the child
through the angular's [property]
syntax for data-binding. So if I want to track some changes of model
in the parent
I can easily add event to the child
and through the (event)
syntax track the changes in the 'parent'. So how can I implement the opposite situation when parent
changes model
and child
want to be notified ?
You can put your additional logic or calculations into onChange method, that is called after all of component's bound properties are updated.
@Component({
selector: 'child',
properties : ['model']
})
@View({
template: `
<p>Child {{ model }}</p>
`,
})
class Child {
model: number;
onChange(map){
if(map.model) {
console.log('doing crazy stuff here');
console.log(map.model); //SimpleChange {previousValue: 43, currentValue: 44}
}
}
}
Plunker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With