[Angular 2] Move and Delete Angular 2 Components After Creation
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to reorder the components. This is especially helpful when you want an event to trigger layout changes from your generated components.
import {
Component, ViewChild, ViewContainerRef, ComponentFactoryResolver
} 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 {
last;
@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);
this.last = this.container.createComponent(WidgetFactory); // return a componentRef
this.last.instance.message = "I am last"; // using componentRef's instance prop to access the component prop
this.last.instance.renderer.setElementStyle(
this.last.instance.input.nativeElement,
'color',
'red'
);
}
onClick(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
const comRef = this.container.createComponent(WidgetFactory, );
comRef.instance.message = "I am third";
}
moveup(){
const randomInx = Math.floor(Math.random() * this.container.length);
this.container.move(this.last.hostView, randomInx);
}
}
In the code, will just move the last WidgetTree component to some random position.
How to get the host elemnt?:
this.last.hostVie
How to get the children number inside the host veiw?
this.container.length
[Angular 2] Move and Delete Angular 2 Components After Creation的更多相关文章
- angular源码分析:angular中各种常用函数,比较省代码的各种小技巧
angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...
- angular源码分析:angular中的依赖注入式如何实现的
一.准备 angular的源码一份,我这里使用的是v1.4.7.源码的获取,请参考我另一篇博文:angular源码分析:angular源代码的获取与编译环境安装 二.什么是依赖注入 据我所知,依赖注入 ...
- angular源码分析:angular中脏活累活的承担者之$interpolate
一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...
- angular源码分析:angular中入境检察官$sce
一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...
- angular的跨域(angular百度下拉提示模拟)和angular选项卡
1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...
- angular源码分析:angular的整个加载流程
在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...
- angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了
一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...
- angular源码分析:angular的源代码目录结构说明
一.读源码,是选择"编译合并后"的呢还是"编译前的"呢? 有朋友说,读angular源码,直接看编译后的,多好,不用管模块间的关系,从上往下读就好了.但是在我看 ...
- angular源码分析:angular源代码的获取与编译环境安装
一.安装git客户端 1.windows环境推荐使用TortoiseGit. 官网地址:http://tortoisegit.org 下载地址:http://tortoisegit.org/downl ...
随机推荐
- C#的默认编码
C# 的所有源代码文件,默认编码为 UTF-8,注意,是源代码文件,而不是 C# 中的 string. C# 中的所有 string,默认编码均为 Unicode (UTF-16). C# 产生的 A ...
- 关系数据库&&NoSQL数据库
在过去,我们只需要学习和使用一种数据库技术,就能做几乎所有的数据库应用开发.因为成熟稳定的关系数据库产品并不是很多,而供你选择的免费版本就更加少了,所以互联网领域基本上都选择了免费的MySQL数据库. ...
- 在Ubuntu下ADT识别不出真机的解决办法
前两天把系统换成Ubuntu 12.04,今天在写代码的时候准备真机调试,结果ADT识别不出真机,我擦.果断网上查找了一下解决办法,经过半个小时左右的折腾,尼玛,终于搞定了.具体解决办法如下: 1.先 ...
- Android Audio遇到播放无声时的分析
在Android Audio开发过程中,有遇到播放ringtone时无声,但播放Music可以听到声音,关于无声问题的分析,在此做个笔记,方便以后回顾. 分析方向: 1:在音量控制面板中确认该音频流对 ...
- HW7.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- linux下使用libiconv库转码
iconv命令实现linux下字符集编码的转换 windows下的文件复制到linux下时常会乱码,因为windows下文件编码为GBK,linux下默认文件编码为UTF-8,故需要libiconv库 ...
- DaoFactory.java
package com; import com.isoftstone.fwk.dao.ActionDao; import com.isoftstone.fwk.dao.Dao; import com. ...
- Java沙箱技术
自从Java技术出现以来,有关Java平台的安全性及由Java技术发展所引发的新的安全性问题,引起了越来越多的关注.目前,Java已经大量应用在各个领域,研究Java的安全 性对于更好地使用Java具 ...
- AJAX制作JSON格式的实时更新数据的方法
之前有写过这样的文章,但是出现了几个问题,第一,如果每秒都像数据库发送请求势必会造成服务器的压力过大,第二,如果使用JS的话,是不可以取得系统时间的,因为JS运行在客户端,所以只能取得客户端时间, 如 ...
- HDU 5794 A Simple Chess (Lucas + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...