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. POJ 3463 有向图求次短路的长度及其方法数

    题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ...

  2. CSS表达式

    一直以来我们被教育说CSS Expression是个坏东西,很影响性能,应该禁止使用,但是如果仔细想想CSS表达式影响性能的原因,规避掉影响性能的写法,CSS表达式还是能给我们带来一些惊喜的.CSS表 ...

  3. zynq学习01 新建一个Helloworld工程

    1,好早买了块FPGA板,zynq 7010 .终极目标是完成相机图像采集及处理.一个Window C++程序猿才开始学FPGA,一个小菜鸟,准备转行. 2,关于这块板,卖家的官方资料学起来没劲.推荐 ...

  4. hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  5. linux下如何使用sftp命令

    sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作. 举例,如远程主机的 IP ...

  6. eclipse 导入工程报错Unable to execute dex: Multiple dex files define Landroid/annotation/SuppressLint

    对策: 检查libs 是否有重复加载的.

  7. 2D几何变换

    2D点:非齐次坐标x(x,y) (x表示向量矢量) 齐次坐标:x~=(x~,y~,w~)=w~(x,y,1)=w~x~         增广矢量:x—=(x,y,1) w~=0时,齐次点称作理想点或无 ...

  8. Windows 10 RTM 官方正式版

    Windows 10 各版本区别: Windows 10 家庭版:供家庭用户使用Windows 10 专业版:供小型企业使用 在家庭版基础上增加了域账号加入.bitlocker.企业商店等功能Wind ...

  9. js⑤

    ##返回在制定位置的的字符  var result = str.charAt(1);  console.log(result);  console.log(str[1]); ##返回在指定的位置的字符 ...

  10. Python的平凡之路(12)

    一.数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储在文件中,但 ...