css – Angular Dynamic Components – 添加类和其他属性

前端之家收集整理的这篇文章主要介绍了css – Angular 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,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">
      });

    }
  }

}

动态组件创建完美,一切都按预期工作.但唯一的问题是我想为动态组件添加一个类,以便它可以

<dynamic-component class="dynamicClass">

任何帮助表示赞赏:(

解决方法

嗯..我通常将它添加到应该是entryComponent的组件的选择器中…
selector: 'dynamic-component.someclass',^^^^^^^^^^^

添加属性使用属性选择器:

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

我称之为entryComponents的隐藏功能

但它的声明性方法并不能在运行时更改(实际上我们可以更改它)

原文链接:https://www.f2er.com/css/242227.html

猜你在找的CSS相关文章