I have a basic angular application with 3 components as below:
- car-component which is the main or parent component.
- carousel-component (child of car-component, *uses ng-content)
- child-component (child of carousel-component)
The button inside the child-component emits an event using the EventEmitter() on click. As you can see from the code in plunker, the car-component (which is the main or master component) is able to listen and capture the event emitted by its child's child component (i.e. child-component.ts) and {{text}} is updated in the car-component template.
However the carousel-component which is the immediate parent of child-component fails to capture the same event and no text is updated inside the carousel-component.
car-component.ts
import {Component} from 'angular2/core';
import {CarouselComponent} from './carousel-component';
import {Child} from './child.component';
@Component({
selector: 'my-app',
template: `
<div style="border:1px solid #CCC; padding: 5px; margin: 5px;">
car-comp txt: {{text}}
<carousel-component>
<child (changed)="onChange($event)"></child>
</carousel-component>
</div>
`,
directives : [Child, CarouselComponent]
})
export class CarComponent {
text: string = '?';
onChange(value) {
console.log('car-component onChange fn..');
this.text = value;
}
}
carousel-component.ts
import {Component, EventEmitter} from 'angular2/core';
import {Output} from 'angular2/core';
@Component({
selector: 'carousel-component',
template: `
<div style="background: #CCC; padding: 10px;">
<div>carousel-component txt: {{carouselTxt}}</div>
<ng-content></ng-content>
</div>
`,
})
export class CarouselComponent {
carouselTxt: string = '?';
changed = new EventEmitter();
onChange(value) {
console.log('carousel-comp onChange called..');
this.carouselTxt = value;
}
}
child.component.ts
import {Component, EventEmitter} from 'angular2/core';
import {Output} from 'angular2/core';
@Component({
selector: 'child',
template: `
<div style="background: #FFF; padding: 10px;">
child-comp txt:
<input type ="text" #textInput value="{{txt}}">
<button (click)="onChange(textInput.value)">Emit</button>
</div>
`,
outputs:['changed']
})
export class Child {
txt: string = 'abc';
changed = new EventEmitter();
onChange(value) {
this.txt = value;
this.changed.emit(value);
}
}
Can someone please let me know where am i going wrong here?