Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular can't use fontawesome in dynamic component

Tags:

angular

I used this post to load dynamically some components

 import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';

 const template =`
    <div class="btn-group">
      <button (click)="play()" class="btn btn-sm btn-outline-info">
        <fa-icon icon="play"></fa-icon>
      </button>

    </div>`;
    const tmpCmp = Component({template: template})(class {

    });
    const tmpModule = NgModule({
      imports: [CommonModule,FontAwesomeModule],
      declarations: [tmpCmp]
    })(class {
    });

    this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
        .then((factories) => {
            const f = factories.componentFactories[0];
            const cmpRef = f.create(this._injector, [], null, this._m);
            cmpRef.instance.name = 'B component';
            this._container.insert(cmpRef.hostView);
        })

When the template is loaded with fa-icon nothing is displayed and no error are returned.

like image 540
lil-works Avatar asked Nov 07 '22 01:11

lil-works


1 Answers

If you want to add an fa class dynamically, I would use [ngClass] to show a specific fa icon because you can use interpolation:

In your component:

public faIcon:string="fa-plane"

I have hardcoded an fa-icon name here, but you can set it however you want.

in your template:

<span class="fa" [ngClass]="{faIcon}}></span>
like image 170
Farasi78 Avatar answered Nov 11 '22 12:11

Farasi78