I am rendering a dynamic page, menu and other items in my application. I also want to change favicon as per configured by admin.
Say, for example, if when my pag
app.module.ts
import {NgModule} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './path/to/app.component';
import {AppService} from './path/to/app.service';
// ....
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
// ...
],
bootstrap: [AppComponent],
providers: [AppService]
})
export class AppModule {}
platformBrowserDynamic().bootstrapModule(AppModule);
app.service.ts
import { Injectable, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class AppService {
constructor(@Inject(DOCUMENT) private _document: HTMLDocument, private http:Http)
getAppDetails(appId: string) {
return this.http.post(url, appId)
.map((response: Response) => {
let details = response.json();
return details;
})
.do(data => console.log(data))
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
setAppFavicon(id: string, basepath: string, icon: string){
this._document.getElementById('appFavicon').setAttribute('href', basepath+"/"+ icon);
}
}
app.component.ts
:
import {Component} from "@angular/core";
import {AppService} from "../path/to/app.service";
@Component({
selector: 'application',
templateUrl: './path/to/app.component.html'
})
export class ApplicationComponent {
details: any;
constructor(private appService: AppService) {
this.details = appService.getAppDetails(id);
appService.setAppFavicon(id,basepath,this.details.icon);
}
}