Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Dynamic Components - Add Class and other attributes

I am using the following code for creating the dynamic components

import {
  Component, OnInit, ViewContainerRef, ViewChild, ViewChildren,
  ReflectiveInjector, ComponentFactoryResolver, ViewEncapsulation, QueryList, Input, AfterViewInit
} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { forEach } from '@angular/router/src/utils/collection';
import { IComponent } from 'app/app.icomponent';


@Component({
  encapsulation: ViewEncapsulation.None,
  selector: 'dynamic-component',
  entryComponents: [HomeComponent, HighlevelSignalComponent],
  template: `
    <div #dynamicDiv [ngClass]="classFromMenu" >
     <ng-template #dynamicComponentContainer></ng-template>
    </div>
  `,
  styleUrls: [
    './dynamic-content.component.css'
  ],
})

export class DynamicComponent implements IComponent, OnInit, AfterViewInit {
  classFromMenu: any;
 @ViewChild('dynamicComponentContainer', { read: ViewContainerRef }) dynamicComponentContainer: ViewContainerRef;


  constructor(private resolver: ComponentFactoryResolver, private route: Router,
    private activatedRoute: ActivatedRoute, ) {

  }

  .......

  buildComponent(passedData) {
   // orderAndObjs has the data for creating the component

      this.orderAndObjs.forEach(obj => {
        var componentFactory = this.resolver.resolveComponentFactory(obj.component);
        var compRef = this.dynamicComponentContainer.createComponent(componentFactory);


     // compRef is the component that is created.
//Assuming the component that i am trying to create is <dynamic-component>. 


  //I want to add either a class or any other attribute like this
//<dynamic-component class="flex">
      });

    }
  }

}

The dynamic-component is created perfectly fine and everything is working as expected. But the only issue is I want to add a class for dynamic-component so that it can be

<dynamic-component class="dynamicClass">

Any help is appreciated :(

like image 759
Shrikey Avatar asked Jan 10 '18 13:01

Shrikey


People also ask

Can we create component dynamically in Angular?

If we go by the Angular definition, a factory component Angular is a base class for a factory that can create a component dynamically. Instantiate a factory for a given type of component with resolveComponentFactory(). Use the resulting ComponentFactory. create() method to create a component of that type.

What can I use instead of ComponentFactoryResolver?

In Angular 13 the new API removes the need for ComponentFactoryResolver being injected into the constructor, like you did in your code. Now to dynamically create a component you have to use ViewContainerRef.

How do I add a class to my host?

You can simply add @HostBinding('class') class = 'someClass'; inside your @Component class. The className directive may also be used and it is better to avoid using class as a variable name (since you might reference it and change it later). Example: @HostBinding('className') myTheme = 'theme-dark'; .


1 Answers

Hmm.. I usually add it to the selector of component that is supposed to be an entryComponent ...

selector: 'dynamic-component.someclass',
                            ^^^^^^^^^^^

to add attribute use attribute selector:

selector: 'dynamic-component[myattr=value]',

I call it hidden feature of entryComponents

but its declarative approach and can't be changed at runtime(indeed we can change it)

like image 157
yurzui Avatar answered Sep 20 '22 01:09

yurzui