简体   繁体   English

在div中动态加载组件 - Angular5

[英]Dynamically load component in div - Angular5

I was able to successfully create a button that on click loads another instance of my component into the DOM, but it loads it outside the component I want it to be inside which results with the CSS being off. 我能够成功创建一个按钮,在单击时将我的组件的另一个实例加载到DOM中,但是它将它加载到我希望它在其中的组件之外,这会导致CSS关闭。 When I add "editorComponents" to my page it currently looks like this: 当我将“editorComponents”添加到我的页面时,它目前看起来像这样:

在此输入图像描述

But I want it to look like this: 但我希望它看起来像这样:

在此输入图像描述

You can see from the inspection that in the second example all of the tags are manually moved to where I want them to be. 您可以从检查中看到,在第二个示例中,所有标签都被手动移动到我想要的位置。

Right now my code is as follows: 现在我的代码如下:

home.component.html home.component.html

 <div class="row backgroundContainer"> <div class="col-md-7 componentContainer"> <app-editor></app-editor> <button (click)="addEditor()" type="button">Add Editor</button> </div> <div class="col-md-5 componentContainer"> <app-bible></app-bible> </div> <div id="footerBox"> <app-footer></app-footer> </div> </div> 

home.component.ts home.component.ts

 import { Component, OnInit, ViewContainerRef, ComponentFactoryResolver } from '@angular/core'; import { EditorComponent } from '../editor/editor.component'; @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.scss'] }) export class HomeComponent implements OnInit { constructor(private componentFactoryResolver: ComponentFactoryResolver, private viewContainerRef: ViewContainerRef) {} ngOnInit() { } addEditor() { const factory = this.componentFactoryResolver.resolveComponentFactory(EditorComponent); const ref = this.viewContainerRef.createComponent(factory); ref.changeDetectorRef.detectChanges(); } } 

So how do I make it so when I dynamically add an EditorComponent to my page it is added directly below the current "app-editor" in my html? 那么当我动态地将EditorComponent添加到我的页面时,如何将它直接添加到我的html中的当前“app-editor”下面呢?

You need to use your own ViewContainerRef . 您需要使用自己的ViewContainerRef Currently you injecting root one and as you can see all your components appended to <app-root> . 目前您正在注入root one,因为您可以看到所有组件都附加到<app-root>

import { Component, OnInit, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
import { EditorComponent } from '../editor/editor.component';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  // Setup custom viewChild here
  @ViewChild('container', { read: ViewContainerRef }) viewContainerRef: ViewContainerRef;    

  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}

  ngOnInit() {
  }

  addEditor() {
    const factory = this.componentFactoryResolver.resolveComponentFactory(EditorComponent);
    const ref = this.viewContainerRef.createComponent(factory);
    ref.changeDetectorRef.detectChanges();
  }

}

Now inside your home.component.html put this where you want your editors to be instantiated 现在你的home.component.html里面放置了你希望你的编辑器被实例化的地方

<div #container></div>

You can use any html tag here. 你可以在这里使用任何html标签。

Now all your editors will be inside this block. 现在你所有的编辑都将在这个街区内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM