动态添加一个component:

  

import {
ViewContainerRef,
Component,
ComponentFactoryResolver,
ViewChild
} from '@angular/core';
import {InstanceComponent} from ' ./ instance.component'; @Component{
selector:'my-app'
template:`
<div #insert>
insert Component
</div>
`
} export class AppComponent{
@ViewChild('insert',{read:ViewContainerRef})insert;
constructor(
private _vcr:ViewContainerRef
private _cfr:ComponentFactoryResolver
){}
ngAfterViewInit(){
let factory=this._cfr.resolveComponentFactory(InstanceComponent); //先转换成factoryComponent类型。
this._vcr.createComponent(factory); // 在本component末尾加入factory;
//或者this.insert.createComponent(factory); // 在#insert内部的末尾加入factory;
}
 clear(){
this._cfr.clear() //清除加入的元素。
}
remove(){ //既然有加入component,那么也得写一个删除的方法。
let node=this.insert.nativeElement;
node.parentNode.removeChild(node);
} }

angular中的NgModule中存在entryComponents属性,一般是不需要配置的。因为被bootStrap直接及递归引导的component和router中定义的component,会被自动添加入entryComponent。

可是我们要加入的这个component,就没有落脚之地,所以你需要自己在app.module中的entryComponents里加入这个component。

下面是一些API: 

一,

@ViewChild:

   @ViewChild('insert',{read:_params});

_params:默认值为ElementRef;另一个可选值为ViewContainer;

@ViewChildren:

   在@ViewChild中填入component,难免该component不止被使用一次。所以在多次出现时,使用viewChildren;

@ContentChild:

  首先你得分清content与view的差别。他们所对应的元素,view是在native element中定义,content是在host element中定义再映射入native中。

@ContentChild(component) ;获得projection component;

@ContentChildren:

  相对于ContentChild选择多个。

使用进阶:

@Directive({
selector:'div',
})export class myDirective{
constructor(
private el:ElementRef;
){}
} @Component ({
template:`
<div id='div1'> one</div>
<div> two </div>
`
})export class AppComponent{
@ViewChildren(myDirective)_md;
ngAfterView(){
this._md.map(node=>{
console.log(node.el.nativeElement.nodeName);
});
    this._md.changes.subscribe(val=>{.....}); }
}
 

@ViewChildren返回一个:QueryList。拥有map方法,但它不属于Array。

 myDirective将<div>绑定为一个Directive,并使其具有.el属性。其中第一个div多带一个id属性。注意,不做injector或者attr,将返回一个空的{}。

 this._md.changes可以监听这些ViewChildren的变化,比如add,move,remove. 

@ContentChildren。使用方法雷同。

二,动态 innerHTML。

ViewContainerRef.createEmbeddedView

@Component ({
template:`
<template #tp>
<h1> templateRef</h1>
</template>
` })export class AppComponent{
@ViewChild('tp')tp;
constructor(private _vcr:ViewContainerRef){};
ngAfterViewInit(){ this._vcr.createEmbeddedView(tp);
} }

 @ViewChild('tp')得到的是一个templateRef;

通过这个方法,可以达成类似Jquery 中,$('<h1> templateRef</h1>').insertAfter('my-app')的效果。

三,私有注入。

  ViewProviders:[];

  angular中除了惰性模块,一旦你使用providers注册服务,那它会立即提升为全局。

   所以在一些特殊情况需要私有服务时,我们使用ViewProviders代替providers,因为ViewProviders内的服务,只能被自身及ViewChildren使用。

------------这个排版不好用啊。有时间自己搞几个class。。。- -!

angular2 笔记的更多相关文章

  1. Angular2笔记:NgModule

    Angular的模块的目的是用来组织app的逻辑结构. 在ng中使用@NgModule修饰的class就被认为是一个ng module.NgModule可以管理模块内部的Components.Dire ...

  2. angular2自学笔记---官网项目(一)

    1.单向数据绑定的'插值表达式' angular中最典型的数据显示方式:把HTML模板(template)的控件绑定到angular组件的属性(component相当于一个构造函数,下面例子中的这个构 ...

  3. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

  4. angular2 学习笔记 ( ngModule 模块 )

    2016-08-25, 当前版本是 RC 5. 参考 : https://angular.cn/docs/ts/latest/guide/ngmodule.html 提醒 : 这系列笔记的 " ...

  5. Angular2之路由学习笔记

    目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...

  6. Angular2学习笔记(1)

    Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...

  7. Angular2开发笔记

    Problem 使用依赖注入应该注意些什么 服务一般用来做什么 指令一般用来做什么 angular2如何提取公共组件 angular2为什么不需要提公共组件 父组件与子组件之间如何通讯 什么时候应该使 ...

  8. Angular2学习笔记——路由器模型(Router)

    Angular2以组件化的视角来看待web应用,使用Angular2开发的web应用,就是一棵组件树.组件大致分为两类:一类是如list.table这种通放之四海而皆准的通用组件,一类是专为业务开发的 ...

  9. Angular2学习笔记——Observable

    Reactive Extensions for Javascript 诞生于几年前,随着angular2正式版的发布,它将会被更多开发者所认知.RxJs提供的核心是Observable对象,它是一个使 ...

随机推荐

  1. imx6 gpio irq

    /***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...

  2. extjs之apply

    ext.apply(Ext.Form.VTypes,{}) {}里面的内容如下: { password:function (val.field) { if(field.initialPassField ...

  3. 导出Excel

    一.asp.net中导出Execl的方法:在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览器 ...

  4. php类的__get和__set方法

    (1)这两个方法是自动调用的 (2)这两个方法只有在成员变量是private的时候才会自己调用 testclass.php <?php class testclass { private $va ...

  5. Scala的第一步

    第一步:学习使用Scala解释器 开始Scala最简单的方法是使用Scala解释器,它是一个编写Scala表达式和程序的交互式“shell”.在使用Scala之前需要安装Scala,可以参考 Firs ...

  6. nim也玩一行流,nim版的list comprehension

    nim 是一门风格类似python的静态编译型语言,官方网站:http://nim-lang.org 如果你想折腾nim的编辑环境,可以用sublime text3 +插件nimlime,notepa ...

  7. goldengate for big data 12.3发布

    主要新增特性:通用JDBC目标端:支持Amazon Redshift & IBM Netezza Oracle GoldenGate for Big Data 12.3现在支持通用的JDBC目 ...

  8. web浏览器兼容简要整理

    ajax的创建 if (window.XMLHttpRequest) { var xhr = new XMLHttpRequest(); } else { //IE6及其以下版本浏览器 var xhr ...

  9. how a 程序猿 doubled his salary?

    One thing i can say, no matter what position i was in or what was my salary, i never stopped studyin ...

  10. Python基础知识之排序法

    在Python开发中,我们会经常使用到排序法,排序的最简单的方法是用sort(list)函数,它接受一个列表并返回与有序的元素一个新的列表. 原始列表不被改变. a = [5, 1, 4, 3]    ...