My solution using "content projection" and data binding through a
BehaviorSubject.
*page-layout.component (the shared HTML)*
<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;
});
}
}
*assembly-list.component (a component using the shared HTML)*
<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');
}
}
*layout-service (the data binding for the shared HTML)*
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);
}
}
On Friday, 21 July 2017 09:22:38 UTC+2, halvorsen wrote:
>
> Hi Sander,
>
> Thanks, exactly what I was looking for. The only problem is that data
> binding does not work I have tried to the simplest possible solution I
> could think of, but maybe I need to use a BehaviorSubject and
> observable/subscribe?
>
> *page-layout.component*
>
> <div style="background-color: red;">
> <h1>{{subtitle}}</h1>
> <ng-content></ng-content>
> </div>
>
> import { Component } from '@angular/core';
>
> @Component({
> selector: 'page-layout',
> templateUrl: './page-layout.component.html',
> styleUrls: ['./page-layout.component.css']
> })
>
> export class PageLayoutComponent {
> subtitle = 'PageLayoutComponent';
> }
>
>
>
> *work-orders.component*
> <page-layout>
> ...
> </page-layout>
>
> import { Component, OnInit } from '@angular/core';
>
> import { PageLayoutComponent } from
> '../../components/page-layout/page-layout.component';
>
> @Component({
> selector: 'work-orders',
> templateUrl: './work-orders.component.html',
> styleUrls: ['./work-orders.component.css']
> })
>
> export class WorkOrdersComponent extends PageLayoutComponent implements
> OnInit {
> ngOnInit(): void {
> this.subtitle = 'WorkOrdersComponent';
> }
>
> constructor() {
> super();
> }
> }
>
> Regards,
> halvorsen
>
> On Friday, 21 July 2017 08:03:26 UTC+2, Sander Elias wrote:
>>
>> Hi Halvorsen,
>>
>> Yes that is possible, The easiest way is adding another components that
>> uses content projection.
>>
>> something like this:
>> import { Component } from '@angular/core';
>>
>> @Component({
>> selector: 'holder',
>> template: `
>> <div class="classB">
>> <h2>{{Subtitle}}</h2>
>> </div>
>> <div class="classC">
>> <ng-content></ng-content>
>> </div>
>> `
>> })
>> export class MyHolderComponent {}
>>
>> @Component({
>> selector: 'howToUse',
>> template: `
>> <holder>
>> Y_SPECIFIC_HTML
>> </holder>
>> `
>> })
>> export class MyHowToUseComponent {}
>>
>> But there are more way's to resolve your issue.
>>
>> Regards
>> Sander
>>
>
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.