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. keep the bar green to keep the code clean——Junit详解(一)

    测试用例 单元测试时每个开发人员必需掌握的,是保证开发过程中代码的准确性,无误性,保证代码质量.敏捷开发模式是先根据用户需求写测试用例,考虑基本所有用户所需要的情况,再写实现方法.单元测试有很多种,当 ...

  2. OC NSString 基本操作(用到补充持续更新)

    1.将字符串拆分成数组 NSString *string = @"1,2,3,4"; NSArray *array = [string componentsSeparatedByS ...

  3. IIC总线解析

    IIC简介: IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司在八十年代初设计出来的,主要是用来连接整体电路(ICS) ,IIC是一种多向控 ...

  4. SpringMVC与MyBatis整合(一)——查询人员列表

    从今天开始,一点点的记录做毕设和学习的过程. 寒假才开始接触SpringMVC和MyBatis,之前对框架的概念理解并不到位,也没学过Spring.目前学习起来思路并不很清晰,有些东西我还不能理解,只 ...

  5. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  6. 张小龙微信小程序演讲内容简介

    1月9号,看看实际上如何

  7. 详解Android中AsyncTask的使用

    在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更 ...

  8. [DNS-BIND]网络初始化

    1.创建ns_g_socketmgr: 首先,套接字管理器是全局唯一的,与有多少个网络接口无关,全局变量定义在/bin/named/include/named/globals.h: EXTERN is ...

  9. spring aop 声明式事务管理

    一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...

  10. NSString,NSArray,NSNumber等类的继承问题

    问题引入,我想给NSString类扩展一些新的方法.在Objective-C中可以有两种方法,一是继承,二是类别.本文先不讨论类别,我们用继承的方法试一下: @interface StringEx : ...