[Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
By default, when you generate components, they will simply be added to the page in order, one after another. Angular 2 provides methods on the ViewContainer that allow you to reorder components once they’ve been created and provide the order that you want.
When you want to create a component and insert it at cetern position of the list. Then what you need to do is just pass in a second param, when you call 'createComponent()'.
import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, Input} from '@angular/core';
import {SimpleService} from "../../serivces/simple.service";
import {WidgetThree} from "../widgets/widget-three.component"; @Component({
moduleId: module.id,
selector: 'home',
templateUrl: 'home.component.html'
})
export class HomeComponent { @ViewChild('container', {
read: ViewContainerRef
}) container; constructor(private resolver: ComponentFactoryResolver, private simpleService: SimpleService) {
} ngAfterContentInit(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
const comRef = this.container.createComponent(WidgetFactory); // return a componentRef
comRef.instance.message = "I am last"; // using componentRef's instance prop to access the component prop
comRef.instance.renderer.setElementStyle(
comRef.instance.input.nativeElement,
'color',
'red'
)
} onClick(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
const comRef = this.container.createComponent(WidgetFactory, );
comRef.instance.message = "I am third";
} }
So when click a button, we wil call 'onClick()' method, which will create a new component and insert it at the third place in the list.
[Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer的更多相关文章
- Angular 个人深究(一)【Angular中的Typescript 装饰器】
Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...
- 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...
- (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...
- [Vue @Component] Switch Between Vue Components with Dynamic Components
A common scenario is to present different components based on the state of the application. Dynamic ...
- angular.module()创建、获取、注册angular中的模块
// 传递参数不止一个,代表新建模块;空数组代表该模块不依赖其他模块 var createModule = angular.module("myModule", []); // 只 ...
- Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目
前言: 最近一直在使用阿里的NG-ZORRO(Angular组件库)开发公司后端的管理系统,写了一段时间的Angular以后发现对于我们.NET后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...
- vue & dynamic components
vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...
- [Angular] Dynamic components with ComponentFactoryResolver
To create a component dynamicly. 1. Add a container with ref: @Component({ selector: 'app-root', tem ...
- [Angular 2] Set Properties on Dynamically Created Angular 2 Components
When you generate Angular 2 components, you’re still able to access the component instance to set pr ...
随机推荐
- 庞锋 OpenCV 视频 学习进度备忘
书签:另外跳过的内容有待跟进 学习资源: opencv视频教程目录(初级) 主讲:庞锋,毕业于电子科技大学 知识基础支持: 线性代数 应用数学 跳过的内容: 1.第1~6集跳过,简单.(2014- ...
- Python单例模式研究
方法一 import threading class Singleton(object): __instance = None __lock = threading.Lock() # used t ...
- C#面向对象基础01
面向对象不是取代面向过程的类.对象."人"是类,"张三"是人这个类的对象.类是抽象的,对象是具体的.按钮就是类,某个按钮就是对象.对象可以叫做类的实例.类就像i ...
- Leetcode: Length of Last Word in python
Length of Last Word Total Accepted: 47690 Total Submissions: 168587 Given a string s consists of ...
- STL源码剖析读书笔记--第6章&第7章--算法与仿函数
老实说,这两章内容还蛮多的,但是其实在应用中一点点了解比较好.所以我决定这两张在以后使用过程中零零散散地总结,这个时候就说些基本概念好了.实际上,这两个STL组件都及其重要,我不详述一方面是自己偷懒, ...
- 使用JavaMail API发送邮件
发送邮件是很常用的功能,注册验证,找回密码,到货通知,欠费提醒等,都可以通过邮件来提醒. Java中发送邮件需要使用javax.mail.jar包,读者可以上网搜索或去官方下载,下载地址为: 下面贴上 ...
- Hadoop学习记录(7)|Eclipse远程调试Hadoop
1.创建Hadoop项目 2.创建包.类 这里使用hdfs.WordCount为例 3.编写自定Mapper和Reducer程序 MyMapper类 static class MyMapper ext ...
- ajax 第一个程序
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- C++11右值引用
[C++11右值引用] 1.什么是左值?什么是右值? 左值是表达式结束后依然存在的对象:右值是表达式结束时就不再存在的对象. 2.std::move的作用是什么? std::move用于把任意类型转化 ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...