在 div 中动态加载组件 - Angular5 [英] Dynamically load component in div - Angular5

查看:29
本文介绍了在 div 中动态加载组件 - Angular5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功创建一个按钮,单击该按钮将我的组件的另一个实例加载到 DOM 中,但是它将它加载到我希望它位于其中的组件之外,结果导致 CSS 关闭.当我将editorComponents"添加到我的页面时,它当前看起来像这样:

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. When I add "editorComponents" to my page it currently looks like this:

但我希望它看起来像这样:

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.

现在我的代码如下:

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

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();
  }

}

那么,当我将 EditorComponent 动态添加到我的页面时,我该如何做到这一点,它会直接添加到我的 html 中当前app-editor"的下方?

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?

推荐答案

你需要使用自己的ViewContainerRef.目前您正在注入 root 之一,您可以看到所有组件都附加到 <app-root>.

You need to use your own ViewContainerRef. Currently you injecting root one and as you can see all your components appended to <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();
  }

}

现在在你的 home.component.html 中把它放在你希望你的编辑器被实例化的地方

Now inside your home.component.html put this where you want your editors to be instantiated

<div #container></div>

您可以在此处使用任何 html 标签.

You can use any html tag here.

现在您的所有编辑器都将在此块内.

Now all your editors will be inside this block.

这篇关于在 div 中动态加载组件 - Angular5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆