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. win7打不开chm格式文件

           近期在开发的过程中,发现重装的系统Wind7 打不开java帮助文档.搜索了半天才找到. 在这里分享一下. 一.假设不能打开,可这样恢复文件关联: 1.開始执行,输入:regsvr32 ...

  2. Beginning iOS Programming

    Beginning iOS Programming 2014年 published by Wrox

  3. GO语言学习(二十)Go 语言递归函数

    Go 语言递归函数 递归,就是在运行的过程中调用自己. 语法格式如下: func recursion() { recursion() /* 函数调用自身 */ } func main() { recu ...

  4. 当鼠标聚焦时输入框变色(focus事件实例)

    当鼠标聚焦时输入框变色css相关,鼠标点击<input>输入域后出现有颜色的边框原理:css伪类之input输入框鼠标点击边框变色效果伪类元素的使用::focus 一:当输入框获得焦点时, ...

  5. 软件——python,主函数

    1;; 如何在spyder中运行python程序 如下图,   写入一个输出  ' hellow word '的程序 然后点击运行按钮就可以运行了.

  6. 【习题 3-8 UVA - 202】Repeating Decimals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...

  7. Dynamic device virtualization

    A system and method for providing dynamic device virtualization is herein disclosed. According to on ...

  8. TTS-零基础入门之语音模板化

    上篇介绍了TTS的一个简单样例http://blog.csdn.net/u010176014/article/details/47326413 本篇咱们进一步聊聊 语音怎样读模板. 比方 公交车上的模 ...

  9. thinkphp事务机制

    thinkphp事务机制 一.总结 下面文章也要看,下面有抛出异常(自己提供错误信息那种) 1.事务机制(原子性):所有的事情都完成了就提交,否则回滚.电商里面用的多,付钱买东西的时候. 2.样例(简 ...

  10. 动态规划 —— 求解通配符问题(wildcard)

    he?p help, heap, √ hellp, × *p*(必须包含 p,左右随意) help, papa, √ hello × *bb*(必须包含连续的两个 bb,左右随意) babbc √ 1 ...