如何在Angular2中的模板HTML上动态创建数组组件



我通过选择预先存在的组件创建了动态组件实例。例如,

@Component({
    selector: 'dynamic-component',
    template: `<div #container><ng-content></ng-content></div>`
})
export class DynamicComponent {
@ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef; 
    public addComponent(ngItem: Type<WidgetComponent>,selectedPlugin:Plugin): WidgetComponent {
    let factory = this.compFactoryResolver.resolveComponentFactory(ngItem);
    const ref = this.container.createComponent(factory);
    const newItem: WidgetComponent = ref.instance;
    newItem.pluginId =  Math.random() + ''; 
    newItem.plugin =   selectedPlugin;
    this._elements.push(newItem);                 
    return newItem;
  }
}

我先前存在的组件是ChartWidget和Disteragetwidget,它扩展了我想在容器中添加的类WidgetComponent。例如,

@Component({
selector: 'chart-widget',
templateUrl: 'chart-widget.component.html',
providers: [{provide: WidgetComponent, useExisting: forwardRef(() => ChartWidget) }]
})
export class ChartWidget extends WidgetComponent implements OnInit {
       constructor(ngEl: ElementRef, renderer: Renderer) {
    super(ngEl, renderer);
    }
    ngOnInit() {}
     close(){
      console.log('close');
    }
    refresh(){
      console.log('refresh');
    }
    ...
}

Chart-Widget.compoment.html(使用Primeng面板(

<p-panel [style]="{'margin-bottom':'20px'}">
    <p-header>
        <div class="ui-helper-clearfix">
           <span class="ui-panel-title" style="font-size:14px;display:inline-block;margin-top:2px">Chart Widget</span>
            <div class="ui-toolbar-group-right">                
               <button pButton type="button" icon="fa-window-minimize" (click)="minimize()"</button>
              <button pButton type="button" icon="fa-refresh" (click)="refresh()"></button>
              <button pButton type="button"  icon="fa-expand" (click)="expand()" ></button>
             <button pButton type="button" (click)="close()" icon="fa-window-close"></button>
                    </div>
                </div>
    </p-header>
      some data
</p-panel>

data-widget.compoment.html(与Primeng面板相同(

@Component({
selector: 'data-widget',
templateUrl: 'data-widget.component.html',
providers: [{provide: WidgetComponent, useExisting: forwardRef(() =>DataWidget) }]
})
export class DataWidget extends WidgetComponent implements OnInit {
       constructor(ngEl: ElementRef, renderer: Renderer) {
    super(ngEl, renderer);
    }
    ngOnInit() {}
    close(){
      console.log('close');
    }
    refresh(){
      console.log('refresh');
    }
    ...
}

widgetcomponent.ts

@Component({
  selector: 'widget',
  template: '<ng-content></ng-content>'
})
export  class WidgetComponent{
}

现在,我通过以下方式从存在的组件(例如Chart-Widget和Data-Widget(中选择一个组件来添加组件,并将这些实例存储到数组中。

@Component({
templateUrl: 'main.component.html',
entryComponents: [ChartWidget,  DataWidget], 
})
export class MainComponent implements OnInit {
private elements: Array<WidgetComponent>=[];
 private WidgetClasses = {
    'ChartWidget': ChartWidget,
    'DataWidget': DataWidget        
}
@ViewChild(DynamicComponent) dynamicComponent: DynamicComponent;  
 addComponent(): void{                         
   let ref= this.dynamicComponent.addComponent(this.WidgetClasses[this.selectedComponent], this.selectedComponent);    
   this.elements.push(ref); 
   this.dynamicComponent.resetContainer();                     
}
}

现在,我面临的问题是在main.component.html中使用innerhtml渲染组件。它呈现HTML,但我无法使用按钮点击事件或其他事件。我还尝试使用Primeng渲染图表,但它也无法正常工作。

main.component.html

 <dynamic-component [hidden]="true" ></dynamic-component>                           
 <widget *ngFor="let item of elements">
     <div [innerHTML]="item._ngEl.nativeElement.innerHTML | sanitizeHtml">
     </div>
 </widget>

我还实施了SanitizeHTML管道,但其结果仍然相同。因此,据我了解,InnerHTML仅显示HTML数据,但我无法使用任何按钮事件以及JS图表。我还试图在标签下显示这样的项目{{item}}。但是它像文本[对象对象]一样显示。那么,有人可以为此提供解决方案吗?如何渲染允许按钮事件和JS图表的组件?谢谢。

编辑:请参阅我的plunker https://plnkr.co/edit/lugu2ppssbd3xhphiup1?p=peview您可以在这里看到,可以动态添加图表或数据窗口小部件,并且我正在使用InnerHTML进行显示。因此,按钮事件在这里不起作用。如果我像{{item}}一样编码,则显示[对象]文本。您还可以在控制台中查看组件数组数据。主要问题是,如何激活其上的按钮事件(例如,如果我单击关闭或刷新按钮,则它将调用相关功能(?

我会创建结构指令,例如:

view.directive.ts

import { ViewRef, Directive, Input, ViewContainerRef } from '@angular/core';
@Directive({
    selector: '[view]'
})
export class ViewDirective {
  constructor(private vcRef: ViewContainerRef) {}
  @Input()
  set view(view: ViewRef) {
    this.vcRef.clear();
    this.vcRef.insert(view);
  }
  ngOnDestroy() {
    this.vcRef.clear()
  }
}

然后

app.component.ts

private elements: Array<{ view: ViewRef, component: WidgetComponent}> = [];
...
addComponent(widget: string ): void{
  let component = this.dynamicComponent.addComponent(this.WidgetClasses[widget]);
  let view: ViewRef = this.dynamicComponent.container.detach(0);
  this.elements.push({view,component});
  this.dynamicComponent.resetContainer();
}

app.component.html

<widget *ngFor="let item of elements">
  <ng-container *view="item.view"></ng-container>
</widget>

所以我刚刚将视图从动态组件容器移动到所需的位置。

plunker示例

相关内容

  • 没有找到相关文章

最新更新