[Angular] Dynamic component's instance and sorting
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的更多相关文章
- [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 ...
- [Angular 2] Component relative paths
Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...
- angular 引入 component 报错
每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...
- [Angular] Test component template
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- Vue dynamic component All In One
Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...
- angular2 学习笔记 ( Dynamic Component 动态组件)
更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...
- angular—— Dynamic Templates
原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...
- [Unit Testing] Angular Test component with required
export default (ngModule) => { describe('Table Item component', () => { let $compile, directiv ...
- [Angular] Dynamic components with ComponentFactoryResolver
To create a component dynamicly. 1. Add a container with ref: @Component({ selector: 'app-root', tem ...
随机推荐
- 最小二乘法,python3实现
https://www.cnblogs.com/BlogOfMr-Leo/p/8627311.html [用的是库函数] https://blog.csdn. ...
- 软件——python,主函数
1;; 如何在spyder中运行python程序 如下图, 写入一个输出 ' hellow word '的程序 然后点击运行按钮就可以运行了.
- (转)xshell无法在vim中复制黏贴
ssh xshell 连接在vim中无法用 ctrl+insert 复制黏贴 修改.vimrc set mouse=c vi的三种模式:命令模式,插入模式,可视模式.鼠标可以启动于各种模式中: The ...
- 驱动学习2-Menuconfig与Kconfig
Menuconfig: 执行make Menuconfig命令是为了产生.config文件 Kconfig: Kconfig中的指令可以改变Menuconfig图形界面中的东西,例如假如leds控制选 ...
- Android(Lollipop/5.0) Material Design(一) 简单介绍
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...
- vivado中basic memory生成
vivado中basic memory生成
- RISC-V评估系列
RISC-V评估系列 RISC-V工具链搭建 SiFive虚拟机分享--提取码:xe1c SiFive SDK函数结构 底层驱动 driver框架 操作系统FreeRTOS移植 FGPA评估 benc ...
- 点滴记录——学习Redis笔记
转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701409 Redis 默认port6379 Redis适用场景 1. 取最新N个数据的操 ...
- php实现变态跳台阶(记忆化递归)
php实现变态跳台阶(记忆化递归) 一.总结 1.本题思路(分类讨论思路,注意初始值和边界值):第一步如果1,那剩下的就是jumpFloorII($number-1)(下面jumpFloorII以j表 ...
- 20、在PC上测试虚拟驱动vivi
在Ubuntu上测试 准备工作:安装xawtv(是一个应用程序,用来在Ubuntu上捕获摄像头数据并显示)sudo apt-get install xawtv 源码xawtv-3.95.tar.gz: ...