【Angular 2+】使用<ng-content>实现嵌入包含(transclusi
1 为什么要用transclusion(嵌入包含)不要被术语嵌入包含所迷惑,下面用一个简单的例子将它说明清楚。 现有一个卡片组件(card component),它由header、body和footer三个部分组成。
比如,我们可以这样使用该组件:
或者这样:
亦或者这样
总之非常方便。 问题来了,我们该如何实现**头部和尾部格式固定,而body中的内容可以动态显示呢? 2 什么是transclusion(嵌入包含)
transclusion是一个方法,允许你定义个固定视图模板的同时,还可以通过
很有意思吧,下面我们就来实现一下。 3 实现嵌入包含transclusion3.1 App结构|- app/ |- app.component.html |- app.component.ts |- app.module.ts |- card.component.ts |- card.component.html |- main.ts |- index.html |- systemjs.config.js |- tsconfig.json 3.2 单插槽的嵌入包含定义组件// card.component.ts import { Component,Input,Output } from '@angular/core'; @Component({ selector: 'card',templateUrl: 'card.component.html',}) export class CardComponent { @Input() header: string = 'this is header'; @Input() footer: string = 'this is footer'; } 组件模板template: <!-- card.component.html --> <div class="card"> <div class="card-header"> {{ header }} </div> <!-- single slot transclusion here --> <ng-content></ng-content> <div class="card-footer"> {{ footer }} </div> </div> 使用组件现在我们已经定义好了一个组件,接下来我们将使用它。 <!-- app.component.html --> <h1>Single slot transclusion</h1> <card header="my header" footer="my footer"> <!-- put your dynamic content here --> <div class="card-block"> <h4 class="card-title">You can put any content here</h4> <p class="card-text">For example this line of text and</p> <a href="#" class="btn btn-primary">This button</a> </div> <!-- end dynamic content --> <card> 最后在根模块的declarations中声明添加即可。 import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { CardComponent } from './card.component'; // import card component @NgModule({ imports: [ BrowserModule ],declarations: [ AppComponent,CardComponent ],// add in declaration bootstrap: [ AppComponent ],}) export class AppModule { } 好了,大功告成,保存并运行吧。 3.3 插槽的选择器
<!-- card.component.html --> <div class="card"> <div class="card-header"> {{ header }} </div> <!-- add the select attribute to ng-content --> <ng-content select="[card-body]"></ng-content> <div class="card-footer"> {{ footer }} </div> </div> 注意,我们添加了 <!-- app.component.html --> <h1>Single slot transclusion</h1> <card header="my header" footer="my footer"> <div class="card-block" card-body><!-- We add the card-body attribute here --> <h4 class="card-title">You can put any content here</h4> <p class="card-text">For example this line of text and</p> <a href="#" class="btn btn-primary">This button</a> </div> <card> 保存并运行,一切照常运行。 3.4 强大的选择器
3.4.1 带值的属性<!-- card.component.html --> ... <ng-content select="[card-type=body]"></ng-content> ... 3.4.2 使用CSS选择器card.component.html ... <ng-content select=".card-body"></ng-content> ... app.component.html ... <div class="card-block card-body">...</div> ... 除此之外,例如 3.4.3 使用HTML标签card.component.html ... <ng-content select="card-body"></ng-content> ... app.component.html ... <card-body class="card-block">...<card-body> ... 但是,你会遇到一个错误:Unhandled Promise rejection: Template parse errors: 'card-body' is not a known element。 // app.module.ts import { NgModule,NO_ERRORS_SCHEMA } from '@angular/core'; // import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { CardComponent } from './card.component'; @NgModule({ imports: [ BrowserModule ],bootstrap: [ AppComponent ],schemas: [ NO_ERRORS_SCHEMA ] // add this line }) export class AppModule { } 3.5 多插槽的嵌入包含<!-- card.component.html --> <div class="card"> <div class="card-header"> <!-- header slot here --> <ng-content select="card-header"></ng-content> </div> <!-- body slot here --> <ng-content select="card-body"></ng-content> <div class="card-footer"> <!-- footer --> <ng-content select="card-footer"></ng-content> </div> </div> app <!-- app.component.html --> <h1>Multi slot transclusion</h1> <card> <!-- header --> <card-header> New <strong>header</strong> </card-header> <!-- body --> <card-body> <div class="card-block"> <h4 class="card-title">You can put any content here</h4> <p class="card-text">For example this line of text and</p> <a href="#" class="btn btn-primary">This button</a> </div> </card-body> <!-- footer --> <card-footer> New <strong>footer</strong> </card-footer> <card> 4 总结我们应该使用哪个选择器呢?属性选择器?html标签?类选择器? 视情况而定。我更偏向于使用属性选择器,因为它更易读。HTML标签易读但需要在元数据中添加 应该尽可能避免使用类选择器,因为不直观。你第一眼看到它时,第一反应不是“ 好了,就到这里!祝coding愉快! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – 如何使用memcache来加速rails
- twitter-bootstrap – Twitter Bootstrap 3如何改
- Scala Play Framework – 运行涉及Akka的测试时,
- shell – 什么是/ dev/null 2>&1?
- init.rc不能通过adb shell修改的原因
- Redis持久化实践及灾难恢复模拟
- angularjs data-ng-app 和ng-app的区别
- 了解SBT,Scala,SBT-Idea和Play框架如何协同工作
- Shell编程(五)脚本语法
- 在angular2中使用app.component.html中的* ngIf更