Angular-2 : Change favicon icon as per configuration

前端 未结 6 1575
你的背包
你的背包 2021-01-31 09:53

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

6条回答
  •  梦谈多话
    2021-01-31 10:34

    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);
    
        }
    
    
    }
    

提交回复
热议问题