<div class="classA">
<router-outlet></router-outlet>
</div>
<div class="classB">
<h2>{{Subtitle}}</h2>
</div>
<div class="classC">
X_SPECIFIC_HTML
</div>
<div class="classB">
<h2>{{Subtitle}}</h2>
</div>
<div class="classC">
Y_SPECIFIC_HTML
</div>
<div class="classB">
<h2>{{Subtitle}}</h2>
</div>
<div class="classC">
INSERT_COMPONENT_HTML_HERE
</div>
const appRoutes: Routes = [
{ path: 'x', component: XComponent },
{ path: 'y', component: YComponent }
];
<div style="background-color: red;">
<h2>subtitle: {{subtitle}}</h2>
<ng-content></ng-content>
</div>
import { Component, OnInit } from '@angular/core';
import { LayoutService } from '../../services/layout.service';
@Component({
selector: 'page-layout',
templateUrl: './page-layout.component.html',
styleUrls: ['./page-layout.component.css']
})
export class PageLayoutComponent implements OnInit {
subtitle = '';
constructor(private LayoutService: LayoutService) {
}
ngOnInit() {
this.LayoutService.observable.subscribe(x => {
if (console) {
console.log('PageLayoutComponent, subscribe: ' + JSON.stringify(x));
}
this.subtitle = x.subtitle;
});
}
}
<page-layout>
<p>ToDo</p>
</page-layout>
import { Component, OnInit } from '@angular/core';
import { LayoutService } from '../../services/layout.service';
@Component({
selector: 'assembly-list',
templateUrl: './assembly-list.component.html',
styleUrls: ['./assembly-list.component.css']
})
export class AssemblyListComponent implements OnInit {
constructor(private LayoutService: LayoutService) {
}
ngOnInit() {
this.LayoutService.emitTitle('AssemblyListComponent1', 'AssemblyListComponent2');
}
}
import { Component, Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
export interface ILayoutServiceData {
title: string;
subtitle: string;
}
@Injectable()
export class LayoutService {
private behaviorSubject: BehaviorSubject<ILayoutServiceData> = new BehaviorSubject({title: null, subtitle: null});
observable = this.behaviorSubject.asObservable();
emitTitle(title: string, subtitle: string) {
if (console) {
console.log(`LayoutService.emitTitle(\'${title}\', \'${subtitle}\'`);
}
const data: ILayoutServiceData = {
title: title,
subtitle: subtitle
};
this.behaviorSubject.next(data);
}
}
Hi Sander,
I'm having a real hard time making the EventEmitter work for my case.
The documentation example is straight forward as you insert the child component as a tag in the parent component's markup.
<my-voter ... (onVoted)="onVoted($event)">
</my-voter>
In my case, the child tag is the ng-content tag in my page-layout.component, but it does of course not work.
<div style="background-color: red;">
<h2>page-layout.subtitle: {{subtitle}}</h2>
<ng-content (onSetTitle)="onSetTitle($event)"></ng-content>
</div>
Then I thought the page-layout tag in the assembly-list.component must be the "real" parent but this doesn't work either.I get a Member 'onSetTitle' is no callable which makes sense.
<page-layout (onSetTitle)="onSetTitle($event)">
<p>ToDo</p>
</page-layout>
I have found a similar problem at the link below, but I don't fully get the solution (can't make it work):
https://stackoverflow.com/questions/43227652/angular-2-ng-content-receive-event-from-child-component
The link above links to a Plunker which looks similar to what I currently got:
http://plnkr.co/edit/Kt7jpEwzKC8ljvKGLbKt?p=preview
Any suggestions?
I thought Angular should speed of my development. I have now spent several hours on a simple problem that could have been solved with a single line of jQuery :-)