[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 ...
随机推荐
- Google Web Toolkit(GWT) 在windows下环境搭建
1.什么是GWT? Google Web Toolkit(简称GWT,读作/ˈɡwɪt/),是一个前端使用JavaScript,后端使用Java的AJAX framework,以Apache许可证2. ...
- Android学习笔记之ViewFlipper
<1>被添加到ViewFlipper中的两个或两个以上的视图之间将执行一个简单的ViewAnimator动画.一次仅能显示一个子视图.如果需要,可以设置间隔时间使子视图像幻灯片一样自动显示 ...
- 4. Vue-Resource / axios 异步插件
安装 cnmp i vue-resource --save (--save 安装到dependencies下) 引用 <script src="node_modules/vue-res ...
- MyCat:对MySQL数据库进行分库分表
本篇前提: mycat配置正确,且能正常启动. 1.schema.xml <table>标签: dataNode -- 分片节点指定(取值:dataNode中的name属性值) rule ...
- CodeVs——T 3305 水果姐逛水果街Ⅱ
http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- 1.1 Python基础知识 - 变量
1.什么是变量? 变量是可以通过变量名访问的内存地址,变量通常是可变的. 2.怎样去定义? 变量格式: 变量名 = "变量值" 例如: name = "Zhanghk&q ...
- Loadrunner--关联详解
当录制脚本时,VuGen会拦截client端(浏览器)与server端(网站服务器)之间的对话,并且通通记录下来,产生脚本.在VuGen的Recording Log中,您可以找到浏览器与服务器之间所有 ...
- 内存问题检查利器——Purify
内存问题检查利器——Purify 一. 引言 我们都知道软件的测试(在以产品为主的软件公司中叫做QA—Quality Assessment)占了整个软件工程的30% -50%,但有 ...
- (嵌入式开发)自己写bootloader之编写第二阶段
内核编译(make)之后会生成两个文件,一个Image,一个zImage,其中Image为内核映像文件,而zImage为内核的一种映像压缩文件,Image大约为4M,而zImage不到2M. ...
- popen:让进程看似文件
popen打开一个指向进程的带缓冲的连接: FILE *fp; fp = popen("ls", "r"); fgets(buf, len, fp); pclo ...