Experimenting with Angular 2, I aim to create a global service that houses an interface. This interface should be modifiable through the HeaderComponent
. Once the user alters the interface via the HeaderComponent
, the change should reflect in another component called ChildComponent
. To achieve this, I followed the guidance provided in this answer on Stack Overflow.
Consider having an interface and two distinct classes, each assigned a different type of the interface.
MyInterface
export interface MyInterface {
key: string,
}
ClassA
import { MyInterface } from './interfaces/my-interface';
export class ClassA {
type = "A";
_interface: MyInterface = {
key: "value1",
};
}
ClassB
import { MyInterface } from './interfaces/my-interface';
export class ClassB {
type = "B";
_interface: MyInterface = {
key: "value2",
};
}
A global service is responsible for implementing this interface.
GlobalService
import { Injectable, EventEmitter } from '@angular/core';
import { ClassA } from './classes/class-a';
import { ClassB } from './classes/class-b';
import { MyInterface } from './interfaces/my-interface';
@Injectable()
export class GlobalService {
public interfaceChanged: EventEmitter<MyInterface>;
_interface: MyInterface;
interfaceList: string[] = [];
interfaces = [
new ClassA(),
new ClassB()
];
selectedInterface: string;
constructor() {
this.interfaceChanged = new EventEmitter<MyInterface>();
for (var i = 0; i < this.interfaces.length; i++)
this.interfaceList.push(this.interfaces[i].type);
this.changeInterface(this.interfaceList[0]);
}
changeInterface(_interface: string): void {
if (this.interfaceList.includes(_interface)) {
this.selectedInterface = _interface;
for (var i = 0; i < this.interfaces.length; i++) {
if (this.interfaces[i].type == this.selectedInterface) {
this._interface = this.interfaces[i]._interface;
this.interfaceChanged.emit(this.interfaces[i]._interface);
}
}
}
}
}
The HeaderComponent
, implemented as a directive in app.component.ts
:
app.component.ts
import { HeaderDirective } from './directives/header';
import { FooterDirective } from './directives/footer';
@Component({
selector: 'my-app',
template: `
<my-header></my-header>
<div class="container">
<router-outlet></router-outlet>
</div>
<my-footer></my-footer>
`,
styleUrls: [ ],
directives: [HeaderDirective, FooterDirective, ROUTER_DIRECTIVES]
})
export class AppComponent { }
has the capability to adjust the interface using a select field:
import { Component } from '@angular/core';
import { MyInterface } from './interfaces/my-interface';
import { LanguageService } from './services/global-service';
@Component({
selector: 'my-header',
template: `
<select (change)="change($event.target.value)">
<option *ngFor=" let _interface of interfaceList ">{{ _interface }}</option>
</select>
`,
})
export class HeaderComponent {
selectedInterface: string;
interfaceList: string[];
_interface: MyInterface;
constructor(private globalService: GlobalService) {
this.selectedInterface = this.globalService.selectedInterface;
this.interfaceList = this.globalService.interfaceList;
this._interface = this.globalService._interface;
}
change(_interface: string) {
this.globalService.changeInterface(_interface);
}
}
After successfully modifying the interface through the HeaderComponent
, I seek to replicate the change in another component named ChildComponent
, displayed through
<router-outlet></router-outlet>
import { Component, EventEmitter } from '@angular/core';
import { GlobalService } from './services/global-service';
import { MyInterface } from './interfaces/my-interface';
@Component({
selector: 'my-child',
template: `
<p>Test: {{ _interface.key }}</p>
`,
})
export class ChildComponent {
private _interface: MyInterface;
constructor(private globalService: GlobalService) {
this.globalService.interfaceChanged
.toPromise()
.then(_interface => {
this.changeLanguage(_interface);
})
.catch(err => {
console.log(err);
});
}
changeInterface(_interface: MyInterface) {
this._interface = _interface;
}
}
Despite achieving success in altering the interface via the HeaderComponent
, I face an issue where the interface does not update for the ChildComponent
. The
changeInterface(interface: MyInterface)
method within my ChildComponent
remains unused. The solution proposed by the user in this response:
...
constructor(globalService: GlobalService) {
globalService.interfaceChanged.subscribe(_interface => this.changeInterface(_interface));
}
...
results in an error in my code editor stating: "Parameter 'interface' implicitly has an 'any' type." What could be wrong here? What am I overlooking?
View it live on Plunker here.