angular2中的内容映射:

App.component:

<my-day>
<my-lucky> </my-lucky>
</my-day>

MyDay.component:

selector:'my-day'
template`<h1>my day </h1>
<ng-content></ng-content>
`

MyLucky.component:

selector:'my-lucky',
template:'<p>One fine day</p> '

MyLucky中的内容就会映射到MyDayComponent的'ng-content'标签内;

@ContentChildren(MyLuckyComponent):;MYDay通过它访问;

------------------------------------------

ng-content

ng-content拥有一个select属性,允许选择性地加载组件。但必须保证,所供选择的组件都放在<my-day></my-day>内部。

:

<ng-content select='[one]'></ng-content> //按属性选择  ;;可以提供属性值:select='[one=*]',按属性值来加载。

选择:<my-lucky one> </my-lucky>          选择具有one属性的;

    <ng-content select='my-lucky'></ng-content>//按标签选择

选择:<my-lucky></my-lucky>

    <ng-content select='.class1'></ng-content>//按CSS选择;

选择:<div class='class1'></div>

在设置了select属性后,可允许一个组件内拥有多个ng-content,各自按须加载。

------------------------------------------------------

可惜ng-content select无法进行动态加载。

 <ng-content select='{{selected}}'> 不行。

  <my-lucky [one]='visibility.lucky'> 不行。

  <my-lucky one='{{visibility.lucky}}'>不行。

好吧,我是这样的:

<my-day>
<my-lucky *ngIf='visibility.lucky'></my-lucky>
<my-wonderfull *ngIf='visibility.wonderfull'></my-wonderfull>
</my-day> <ng-content> </ng-content> //不写任何属性。

  

refer:https://scotch.io/tutorials/angular-2-transclusion-using-ng-content

我的Github例子:https://github.com/zhantewei2/Ionic2-example-notes/tree/master/pages/testSegment

angular2 - content projection-的更多相关文章

  1. [Angular] Learn Angular Multi-Slot Content Projection

    Now for au-modal component, we pass in tow component though contenct projection: <au-modal class= ...

  2. [Angular] Configurable Angular Components - Content Projection and Input Templates

    We are going to have a modal component: <au-modal > </au-modal> And we can pass default ...

  3. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  4. [Angular] Content Projection with ng-content

    For example there is tow form compoennts on the page, and what we want to do is reusing the form com ...

  5. Angular2.0的项目架构

    Angular2.0的项目架构 一.项目服务端app a) Controller控制器 b) Router路由 c) Service服务 d) Public公共样式及脚本和图片等静态资源 e) Vie ...

  6. C#版的mongodb最新的官方驱动2.4.0版本

    已经升级了mongodb至最新的3.4的版本,我想想也该把驱动升到最新的了,我用的驱动还是1.7的版本呢,之前几次想升级,都是因为升级驱动需要改动的代码太大了,升级的成本很高,所以懒得动,就这么的用了 ...

  7. Angular:利用内容投射向组件输入ngForOf模板

    现在,我们写一个组件puppiesListCmp,用于显示小狗狗的列表: //puppies-list.component.ts @Component({ selector: 'puppies-lis ...

  8. angular5 @viewChild @ContentChild ElementRef renderer2

    @viewChild 作用一:选择组件内节点 <!--视图 --> <div #mydiv><input></div> // 选择 @ViewChild ...

  9. Angular4学习笔记(八)- ng-content

    内容投影 ng-content ng-content是一个占位符,有些类似于router-outlet. 以前举の例子,父组件包含子组件都是直接指明子组件的selector,比如子组件的selecto ...

随机推荐

  1. 事件(event),正则

    1.事件(event):事件是可以被 JavaScript 侦测到的行为.网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.2.事件源: 触发事件的元素 事件: 被 JavaS ...

  2. linux内核学习之七 可执行程序的装载和运行

    一 程序的装载和运行的基本知识补充    1 当进程开始执行一个新的程序时,从父进程继承的所有页被释放,以便在新的用户地址空间开始执行新的计算,甚至进程的特权都可能发生改变,但是,进程的PID不会改变 ...

  3. 【转】关于Jquery中ajax方法data参数用法的总结

    $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Bosto ...

  4. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...

  5. java 终端练习

    Java第一天笔记 一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始  ---->所有程序--->附件---->命令提示符 Win7--&g ...

  6. Android 2016新技术

    Android 2016新技术 版权声明:本文为博主原创文章,未经博主允许不得转载. 2016你需要了解Android有以下新兴的技术与框架,有些也许还不成熟,但是你应该去了解下,也许就是未来的方向. ...

  7. volatile关键字和mutable关键字

    如果不用volatile关键字会如何?可能会造成一个后果就是:编译器发现你多次使用同一个变量的值,然后它可能会假设这个变量是不变的值,并且把这个变量的值放入寄存器中,方便下一次使用,提高存取速度. 一 ...

  8. android的apk逆向工程后的文件目录介绍

    一.用压缩软件打开apk,得到五个文件或文件夹 1.META-INF 2.res 3.AndroidManifest.xml 4.classes.dex 5.resources.arsc 二.xml文 ...

  9. 第7章 LED将为我闪烁:控制发光二极管

    所谓I/O内存是通过各种接口连接到主机的硬件在主机内存的映射.LED驱动还提供了两种交互方式:命令和读写设备文件. 创建设备文件的步骤: 第1步:使用cdev_init函数初始化cdev 第2步:指定 ...

  10. 背景建模post_processing常用opencv函数(怒了)

    1.saturate_cast<uchar>来说,就是把数据转换成8bit的0~255区间,负值变成0,大于255的变成255.如果是浮点型的数据,变成round最近的整数 2.cv::M ...