angular2 笔记
动态添加一个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 笔记的更多相关文章
- Angular2笔记:NgModule
Angular的模块的目的是用来组织app的逻辑结构. 在ng中使用@NgModule修饰的class就被认为是一个ng module.NgModule可以管理模块内部的Components.Dire ...
- angular2自学笔记---官网项目(一)
1.单向数据绑定的'插值表达式' angular中最典型的数据显示方式:把HTML模板(template)的控件绑定到angular组件的属性(component相当于一个构造函数,下面例子中的这个构 ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- angular2 学习笔记 ( ngModule 模块 )
2016-08-25, 当前版本是 RC 5. 参考 : https://angular.cn/docs/ts/latest/guide/ngmodule.html 提醒 : 这系列笔记的 " ...
- Angular2之路由学习笔记
目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- Angular2开发笔记
Problem 使用依赖注入应该注意些什么 服务一般用来做什么 指令一般用来做什么 angular2如何提取公共组件 angular2为什么不需要提公共组件 父组件与子组件之间如何通讯 什么时候应该使 ...
- Angular2学习笔记——路由器模型(Router)
Angular2以组件化的视角来看待web应用,使用Angular2开发的web应用,就是一棵组件树.组件大致分为两类:一类是如list.table这种通放之四海而皆准的通用组件,一类是专为业务开发的 ...
- Angular2学习笔记——Observable
Reactive Extensions for Javascript 诞生于几年前,随着angular2正式版的发布,它将会被更多开发者所认知.RxJs提供的核心是Observable对象,它是一个使 ...
随机推荐
- 痛苦的vsftpd配置
1.下载安装:yum install vsftpd 2.添加用户和组(不一定要添加组) group -g 1010 customedname useradd -g customedname -d /h ...
- c——I/O Multiplexing笔记
1. select第一个参数为最大FD(int)+1,因为虽然select参数里有三个set,但分配到的fd值是不会重复的,当select检查fd可用时(可读或可写或异常),会遍历进程fd表,这时遍历 ...
- 【CSS3 入门教程系列】CSS3 Media Queries 实现响应式设计
在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...
- JS Math.max() 函数
Math.max(a,b,...,x,y) -- 返回数个数字中较大的值 max是maximum的缩写,中文"最大量"的意思 max函数语法Math.max(a,b,...,x,y ...
- Android 振动器
今天介绍一下Android的振动器Vibrator,有三个方法来控制手机振动: 1.void vibrate(long milliseconds):控制手机振动milliseconds毫秒. 2.vo ...
- php类的__get和__set方法
(1)这两个方法是自动调用的 (2)这两个方法只有在成员变量是private的时候才会自己调用 testclass.php <?php class testclass { private $va ...
- Android新手踩到的一些雷,记录一下~
这两天在搞一个android的app ,由于是新手,注定要踩很多坑. 这里是一些遇到的一些问题,网上各种找答案,感谢各位博主.这里记录一下,留待以后再看. 1. android layout_weig ...
- :before :after
CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...
- 《2---关于JDBC编程过程中驱动配置问题》
说明:我在Editplus中编写了一个简单的JDBC程序,用来测试是否和数据库连接正确.读者如有其它疑问,可以留言交流. [1]程序如下: import java.sql.*; public clas ...
- mySQL基本操作学习笔记(一)
...