I am trying to update app favicon in my angular 2 app by creating <link>
tag in index.html
by doing:
<head>
<base href="/" />
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link id="appFavicon" rel="icon" type="image/png" href="assets/images/teal.png"></link>
</head>
However, the transpiled DOM does not have my link
tag. It deletes the <link>
row.
This causes error:
core.es5.js:1084 ERROR TypeError: Cannot read property 'setAttribute' of null
when I try to update, the favicon in the app when doing something like:
this._document.getElementById('appFavicon').setAttribute('href', 'assets/images/red.png');
my component constructor has :
@Inject(DOCUMENT) private _document: any)
I have tried by using closed and non closed link tags as well:
<link id="appFavicon" rel="icon" type="image/png" href="assets/images/teal.png">
and
<link id="appFavicon" rel="icon" type="image/png" href="assets/images/teal.png"/>
I also tried to create a link element in onInit
method as well, and that also does not get added to DOM.
Please suggest how to add <link>
to DOM, or some alternate methods to update favicon.