[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 ...
随机推荐
- 3lession-python编程规范
今天继续学习python,内容主要就是python编程过程中的一些规范,包括注释.换行等 1.注释 所有的注释都是以#开头,注释可以单独占有一行,也可以放到语句的末尾.因为python没有多行注释,所 ...
- JdbcTemplate简单介绍
转自:http://www.php.cn/java-article-368819.html 一.关于JdbcTemplate JdbcTemplate是最基本的Spring JDBC模板,这个模板支持 ...
- (转) 如何取消Linux下,vi中显示的^M符号
转自:http://www.cnblogs.com/dkblog/archive/2012/02/03/2337187.html [背景知识] ^M 是ascii中的'\r', 回车符,是16进制的0 ...
- 16.用Spring Boot颠覆Java应用开发
转自:https://www.cnblogs.com/aishangJava/p/5971288.html Java开发概述: 使用Java做Web应用开发已经有近20年的历史了,从最初的Servle ...
- Javascript和jquery事件--滚动条事件和自定义滚动条事件样式
很想把滚动条事件跟鼠标滚轮事件放在一起,那就直接写在这一篇了.除了事件以外,对滚动条样式的调整也记在这里吧. 滚动条是浏览器的默认事件,使用overflow:auto/scroll都有可能出现,它的默 ...
- 2019年Angular7——安装搭建路由
Angular 中文官方:https://www.angular.cn/ 为什么要看Angular?我也不知道,因为公司有个Angular的项目要维护.听说Angular的版本已经到7了.以前没怎么玩 ...
- 用CSS实现阴阳八卦图等图形
CSS还是比较强大的,可以实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...
- BZOJ 2245 SDOI 2011 工作安排 费用流
题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...
- 8、linux下输入子系统
input_sync(button_dev); /*通知接收者,一个报告发送完毕*/ 参考:http://www.51hei.com/bbs/dpj-27652-1.html 很详细说明 in ...
- mysql 5.7 innodb count count(*) count(1) 大数据 查询慢 耗时多 优化
原文:mysql 5.7 innodb count count(*) count(1) 大数据 查询慢 耗时多 优化 问题描述 mysql 5.7 innodb 引擎 使用以下几种方法进行统计效率差不 ...