[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 ...
随机推荐
- 第二十八天 月出惊山鸟 —Spring的AOP
6月13日,阴转细雨."人闲桂花落.夜静春山空. 月出惊山鸟.时鸣春涧中." 无论在面向过程还是在面向对象里,奇妙的"纯"字,似乎永远都充满了无限的可能性.除了 ...
- 整理wmic使用,不重启变环境变量 .
整理wmic使用,不重启变环境变量 . 利用wmic修改是直接生效的:(e:\tools 是新添加的目录) wmic ENVIRONMENT where "name='path' and u ...
- drawable-图片绘制
首先看一下,下端代码 private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) { int height = ...
- 关于Promise的详细总结
1. 异步回调 1.1 回调地狱 在需要多个操作的时候,会导致多个回调函数嵌套,导致代码不够直观,就是常说的回调地狱 1.2 并行结果 如果几个异步操作之间并没有前后顺序之分,但需要等多个异步操作都完 ...
- Android开发之搜芽项目的图片载入问题(使用Volley进行网络图片载入)
搜芽的移动开发这几天进度相对来说很的快. 可是美中不足的就是网络图片的载入问题. 我有两套方案: 1)沿用迅雷动漫的图片载入.迅雷动漫也是用的一个开源的库.可是不知道是我使用出了问题还是真的是它的问题 ...
- Excel Add-in
Excel Add-in 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这块 —— 到底是要每 ...
- Codeforces Round #234 (Div. 2):B. Inna and New Matrix of Candies
B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...
- 基于Qt Assistant制作软件帮助文档
Qt Assistant是Qt自带的一款可定制.可重新发行的帮助文件浏览器.Qt Assistant支持HTML文件,用户可以利用其定制自己的功能强大的帮助文档浏览器.关于Qt Assistant定制 ...
- Linux平台Makefile文件的编写基础篇
目的: 基本掌握了 make 的用法,能在Linux系统上编程. 环境: Linux系统,或者有一台Linux服务器,通过终端连接.一句话:有Linux编译环境. 准备: ...
- 每日技术总结:vue router传参方式,js获取设备高度
今天貌似没什么问题,23333…… 1.vue router 路由传参的方式 应用情景:从分类页(category.vue)进入商品列表页(list.vue),需要传递商品分类id(catId),商品 ...