动态添加一个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. 封装获取dom元素

    <script> //函数: 反复执行的代码块 //全局只有一个对象,防止全局变量污染 var itcast = { getElen : { tag : function(tag){ re ...

  2. SQL数据库,使用事务执行增删改操作,给自己一个后悔的机会

    内容并不复杂,使用起来也比较简单. 主要使用以下3条SQL语句: 开始事物:BEGIN TRAN(全拼 TRANSACTION 亦可)提交事物:COMMIT TRAN回滚事务:ROLLBACK TRA ...

  3. 关于Erlang中的behaviour

    唔,听说过这四个牛逼渣渣的behaviour:gen_server,gen_fsm,gen_event,supervisor.所以也就更加好奇behaviour的实现. 在解释它是怎么工作的之前,我们 ...

  4. MFC listcontrol导出excel表格

    // 导出ExcelCString CBackGroundDlg::ExportAsExcel(CString filename, CListCtrl &resultlist, CWnd * ...

  5. 不使用ASP.NET中的服务器控件将如何上传文件?

    遇到文件的上传时,可能会有大部分的开发者喜欢使用服务器控件,虽然很方便,但是却不能很好的控制,不具灵活性. 现给出例子,使用html标签语言灵活的控制文件的上传. 1.html部分 <input ...

  6. Longest Substring Without Repeating Characters(Difficulty: Medium)

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  7. SQL变量、Substring、charindex、case函数、去除重复

      isnull(aa,0)删除表数据: truncate table aaa 添加字段: ALTER TABLE table1 ADD col1 varchar(200) DEFAULT '2008 ...

  8. Fiddler-3 配置Fiddler监听iphone的http/https请求

    电脑端可以通过Fiddler监听手机端的http请求.需要两个步骤:首先配置Fiddler,再配置手机端. 1 配置 Fiddler 允许远程设备连接: 菜单Tools - Telerik Fiddl ...

  9. php 网页 301 跳转

    php 跳转 if('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] =="http://www.nikemalaysia.co ...

  10. 统计字符串”aaaabbbccccddfgh”中字母个数以及统计最多字母数

    function count(){ var str="shhkfahkahsadhadskhdskdha"; var obj={}; for(var i=0;i<str.le ...