How to refresh Font Awesome icons on Angular?


Dumbo

Is there a way to dynamically change font awesome icons? I want the user to be able to dynamically choose one of the font awesome icons. It only works the first time you add a class. Where I try to do this is - MatDialog. There are forms where the user has to choose an icon, background color and category name. To select the icon the user should open another dialog.

enter image description here enter image description here

I am using Angular 9.1.4and Font Awesome 5.13.0.


This is what I have tried:

1. Using ngClass

categorydialog.component.html

<div [ngStyle]="selectedColor">
    <i [ngClass]="selectedIcon"></i>
</div>

categorydialog.component.ts

openIconDialog(): void {
  const dialogRef = this.dialog.open(DialogIconSelectComponent, { width: '15rem' });
  dialogRef.afterClosed().subscribe(result => {
    this.selectedIcon = result;
  });
}

This only works the first time. But when you try to change the icon selectedIconchange, the UI doesn't refresh the element class.


2. Using @ViewChild

@ViewChild('iconElement') iconElement: ElementRef;

constructor(private dialog: MatDialog,
            private renderer: Renderer2) { }

openIconDialog(): void {
  const dialogRef = this.dialog.open(DialogIconSelectComponent, { width: '15rem' });
  dialogRef.afterClosed().subscribe((result: string) => {
    this.iconElement.nativeElement.className = result;
  });
}

This also only works the first time.


3. Using @ViewChild and Renderer2

categorydialog.component.html

<div #colorElement [ngStyle]="selectedColor">
    <i #iconElement></i>
</div>

categorydialog.component.ts

@ViewChild('colorElement') parentElement: ElementRef;
@ViewChild('iconElement') childElement: ElementRef;

constructor(private dialog: MatDialog,
            private renderer: Renderer2) { }

openIconDialog(): void {
  const dialogRef = this.dialog.open(DialogIconSelectComponent, { width: '15rem' });
  dialogRef.afterClosed().subscribe(result => {
    this.replaceIcon(result);
  });
}

replaceIcon(iconClass: string): void {
  const i = this.renderer.createElement('i');
  this.renderer.setProperty(i, 'class', iconClass);
  this.renderer.removeChild(this.parentElement.nativeElement, this.childElement);
  this.renderer.appendChild(this.parentElement.nativeElement, i);
}

That simply doesn't work.


Is there any way to dynamically change font awesome?

Dumbo

solve

Wasted a lot of my free time researching how to solve this problem. Tried Renderer2all dirty Javascript methods. But one day I figured out using innerHtml.

Rendering a new inner HTML string interactively changes the Font Awesome icon.

categorydialog.component.html

<div [ngStyle]="selectedColor" [innerHtml]="selectedIconHtml"></div>

categorydialog.component.ts

openIconDialog(): void {
  const dialogRef = this.dialog.open(DialogIconSelectComponent, { width: '15rem' });
  dialogRef.afterClosed().subscribe((result: string) => {
    // EVERY TIME NEW ELEMENT WITH NEW FA CLASS
    this.selectedIconHtml = `<i class="${result}"></i>`;
  });
}

This solution - on changing <div>element content (inner html) for each icon selection.

Related


How to round font awesome icons?

Dakota Island I'm trying to round icons with font awesome. Please refer to - http://jsfiddle.net/JfGVE/1704/ The problem is that round icons are more elliptical anyway. Can't make it round perfectly. Below is the CSS code I have - i { display: inline-block; ba

How to debug awesome font icons?

How beautiful I've been using Font-Awesome and its icons for a while now and it's working fine. However, today, it just shows blank squares instead of icons. I have read many other related questions but none of these cases apply to me. Like I said, it worked b

How to round font awesome icons?

Dakota Island I'm trying to round icons with font awesome. Please refer to - http://jsfiddle.net/JfGVE/1704/ The problem is that round icons are more elliptical anyway. Can't make it round perfectly. Below is the CSS code I have - i { display: inline-block; ba

How to round font awesome icons?

Dakota Island I'm trying to round icons with font awesome. Please refer to - http://jsfiddle.net/JfGVE/1704/ The problem is that round icons are more elliptical anyway. Can't make it round perfectly. Below is the CSS code I have - i { display: inline-block; ba

How to add font awesome icons?

Sachin Kumar Font Awesome icons not loading this is my code <!DOCTYPE html> <html> <head> <title>Login Page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="http

How to debug awesome font icons?

How beautiful I've been using Font-Awesome and its icons for a while now and it's working fine. However, today, it just shows blank squares instead of icons. I have read many other related questions but none of these cases apply to me. Like I said, it worked b

How to debug awesome font icons?

How beautiful I've been using Font-Awesome and its icons for a while now and it's working fine. However, today, it just shows blank squares instead of icons. I have read many other related questions but none of these cases apply to me. Like I said, it worked b

How to round font awesome icons?

Dakota Island I'm trying to round icons with font awesome. Please refer to - http://jsfiddle.net/JfGVE/1704/ The problem is that round icons are more elliptical anyway. Can't make it round perfectly. Below is the CSS code I have - i { display: inline-block; ba

How to round font awesome icons?

Dakota Island I'm trying to round icons with font awesome. Please refer to - http://jsfiddle.net/JfGVE/1704/ The problem is that round icons are more elliptical anyway. Can't make it round perfectly. Below is the CSS code I have - i { display: inline-block; ba

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

Font Awesome Icons in HTML Input with Angular

mark I'm trying to do this with no luck: <input type="button" class="naviaBtn naviaBlue" ng-show="ppt.Globals.hasDebitCard" ng-click="alertShow = (alertShow == 2 ? -1 : 2)" value="outstanding swipes"><i class="fa fa-exclamation-circle" ng-show="ppt.Swipes !=

How to use icons in WPF [Font-awesome]

Swani: I am new to WPF. I want to use font awesome icons in textboxes and buttons. but the icon is not bound to my textbox I installed Font-awesome resource into my app. let me know how to use it thanks, I really need it, please help me. example Step One: Down

How to Switch Between Font Awesome Icons

username I want to switch awesome icons between fonts. $(document).ready(function () { $("#minus").click(function () { $("#tog").slideToggle(); if ( $("#minus").hasClass("fas fa-minus")) { $("#minus").addClass("fas fa-plus"

How to get Png file of Font awesome icons?

丹妮拉·曼图洛夫(Danila Manturov) I do unity game and I need icons. I think icons with awesome fonts are the best. How can I get the .png files of these icons? I can't put CSS in unity game. I tried downloading other icons, but the images are not good. Mohideen bin Mo

How to offset overlapping Font Awesome icons?

Davsla Is there a way to overlap two Font Awesome icons so that they look like this icon (relative to this icon)? I don't want to stack icons, but rather have one cover like 50% of the other. Thanks!filesfile Matthew Why not just use CSS padding to offset one

How to Create Icons Like Font Awesome

unbreakable I am new to web development. In my example project, to get the icons, I included the font-awesome plugin ( http://fontawesome.io/icons/ ). I can select all available icons. But I have two or three project specific imageslike project logo. Question

How to Vertically Align Font Awesome Icons

Ash I'm using font-awesome for the battery, but it's aligned horizontally. I need it vertically. I know. M use this <i class="fa fa-battery-full" aria-hidden="true"></i> actual results This is the expected result Swapnir Patil .fa.fa-battery-full { -w

How to use icons like Font Awesome in Gatsby

Joe Beso I want to use Font Awesome Icons in my Gatsby project. I would love to have awesome fonts in the CDN. Just including it in a script tag won't work. I think I need to import it, import ... from '../fontawesome.css'but I can't get it to work and would l

How to fix flickering font awesome icons?

BG Bruno If I recommend it everywhere - use a CDN add - on for " Font Awesome " every time it flashes "page load" . <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" /> Q How to prevent flickering? BG Br

How to change the color of icons in Font Awesome 5?

Ed Joe I can't use these codes to colorize Font Awesome 5 icons. I tried fillsetting the color using css properties, but it didn't work. HTML code: <div class="container mt200 icons"> <div class="col-md-3"> <div class="bggray2 text-center"> <i clas