After create a component dynamic, we are able to change the component's props and listen to its event.

  component: ComponentRef<AuthFormComponent>;

  ngAfterContentInit() {
const factory = this.resolver.resolveComponentFactory(AuthFormComponent);this.component = this.entry.createComponent(factory);
this.component.instance.title = "Create new User";
this.component.instance.submitted.subscribe(this.loginUser);
} loginUser(user: User) {
console.log('Login', user);
}

If we log out 'this.component.instance', we can see:

And in AuthFormComponent, we have:

export class AuthFormComponent {

  title = 'Login';

  @Output() submitted: EventEmitter<User> = new EventEmitter<User>();

  onSubmit(value: User) {
this.submitted.emit(value);
} }

After creating a component dynamicly, we also want to see how to remove the dynamicly created component:

  destroyComponent() {
if(this.component) {
this.component.destroy();
}
}

It is really simple, just need to call 'destroy()' api.

One thing to notice here is that, after we call destroy(), the this.component instance is still there, it won't be removed.

If you do want to clean it you can use 'onDestroy()' method to clean it:

    this.component.onDestroy(() => {
delete this.component;
})

Reordering the component:

When you create a component, the default index is '-1'.

If you set to '0', then it will become first, then 1,2,3...

If you want to change the order dynamicly, you can do:

  @ViewChild('entry', {read: ViewContainerRef}) entry: ViewContainerRef;

  component: ComponentRef<AuthFormComponent>;  

moveFirstToBottom() {
if(this.component) {
this.entry.move(this.component.hostView, ); // move to second place
}
}

import {Component, ViewContainerRef, ViewChild, ComponentFactoryResolver, AfterContentInit, ComponentRef} from '@angular/core';

import { AuthFormComponent } from './auth-form/auth-form.component';

import { User } from './auth-form/auth-form.interface';

@Component({
selector: 'app-root',
template: `
<div>
<button (click)="destroyComponent()">Destroy</button>
<button (click)="moveFirstToBottom()">Move first to bottom</button>
<div #entry>
<!-- We want to create auth-form component inside this div-->
</div>
</div>
`
})
export class AppComponent implements AfterContentInit{ @ViewChild('entry', {read: ViewContainerRef}) entry: ViewContainerRef; component: ComponentRef<AuthFormComponent>; constructor(private resolver: ComponentFactoryResolver) { } ngAfterContentInit() {
const factory = this.resolver.resolveComponentFactory(AuthFormComponent);
this.entry.createComponent(factory);
this.component = this.entry.createComponent(factory, );
this.component.instance.title = "Create new User";
this.component.instance.submitted.subscribe(this.loginUser); this.component.onDestroy(() => {
delete this.component;
})
} destroyComponent() {
if(this.component) {
this.component.destroy();
}
} moveFirstToBottom() {
if(this.component) {
this.entry.move(this.component.hostView, );
}
} loginUser(user: User) {
console.log('Login', user);
} }

[Angular] Dynamic component's instance and sorting的更多相关文章

  1. [Angular] Dynamic component rendering by using *ngComponentOutlet

    Let's say you want to rending some component based on condition, for example a Tabs component. Insid ...

  2. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  3. angular 引入 component 报错

    每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...

  4. [Angular] Test component template

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

  5. Vue dynamic component All In One

    Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...

  6. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  7. angular—— Dynamic Templates

    原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...

  8. [Unit Testing] Angular Test component with required

    export default (ngModule) => { describe('Table Item component', () => { let $compile, directiv ...

  9. [Angular] Dynamic components with ComponentFactoryResolver

    To create a component dynamicly. 1. Add a container with ref: @Component({ selector: 'app-root', tem ...

随机推荐

  1. go package的理解

    golang package是基本的管理单元, 同一个package下面,可以有非常多的不同文件,只要 每个文件的头部    都有 如 "package xxx" 的相同name, ...

  2. Oracle调用Java类开发的存储过程、函数的方法

    oracle调用java类的基本步骤 1. 编写java代码,后续可以直接使用java代码,class文件或者jar包 2. 将写好的java代码导入到oracle数据库中,有两种方法:一种是使用lo ...

  3. iOS_04_数据类型、常量、变量

    一.数据 1.什么是数据 * 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据,图片数据,视频数据,还有聊天QQ产生的文 ...

  4. django-rest-framework框架 第三篇 之CRUD视图扩展类(增删改查的优化)

    CRUD视图扩展类 1 CreateModelMixin 2 RetrieveModelMixin 3 UpdateModelMixin 4 DestroyModelMixin <1> 创 ...

  5. [求助]linux同一目录可否挂载多个数据盘?

    https://bbs.aliyun.com/read/281222.html?pos=20

  6. FTP、WEB虚拟目录作用

    随风原文FTP.WEB虚拟目录作用 在 IIS中,双击您要为之添加虚拟目录的服务以显示其属性表.    单击“目录”选项卡.    单击“添加”.    单击“浏览”从“目录”框中选择一个目录.    ...

  7. amazeui页面分析2

    amazeui页面分析2 一.总结 1.弄清结构:这些部分都是一块一块分好了的,掌握结构之后,想替换哪块就替换哪块,想不要哪块就不要哪块,非常简单的 2.一块一块:替换十分简单 3.弄清楚大块之后,然 ...

  8. spring boot 2.x Path with "WEB-INF" or "META-INF"

    学习spring boot 2.x时,使用jsp作为前端页面.在application.properties配置了jsp所在位置 spring.mvc.view.prefix:/WEB-INF/vie ...

  9. 从零开始使用git第三篇:git撤销操作、分支操作和常见冲突

    从零开始使用git 第三篇:git撤销操作.分支操作和常见冲突 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:gi ...

  10. POJ 1833 生成排列

    题目链接:POJ 1833 /************************************ * author : Grant Yuan * time : 2014/10/19 16:38 ...