For example there is tow form compoennts on the page, and what we want to do is reusing the form component. Make to tow form behave differently, we can using <ng-content> inside form and pass what we want from the parent component.

// app.component.ts

    <div>
<auth-form
(submitted)="createUser($event)">
<h3>Create account</h3>
<button type="submit">
Join us
</button>
</auth-form> <auth-form
(submitted)="loginUser($event)">
<h3>Login</h3>
<button type="submit">
Login
</button>
</auth-form>
</div>

For each form we have different event handler such as 'createUser' and 'loginUser'. Besides that for each form we pass one h3 tag and one button tag.

To see how it should looks like:

Now let's see how to write form component to make this happen.

// auth-form.component.ts

    <div>
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select="h3"></ng-content>
<label>
Email address
<input type="email" name="email" ngModel>
</label>
<label>
Password
<input type="password" name="password" ngModel>
</label>
<ng-content select="button"></ng-content>
</form>
</div>

<ng-content> has 'select' attr, which is similar to css selector, you can use component, class, id...


The way I prefer is attribute selector:

      <auth-form
(submitted)="createUser($event)">
<h3 auth-form-title>Create account</h3>
<button auth-form-submit type="submit">
Join us
</button>
</auth-form>

So we you can use it like:

    <div>
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select="[auth-form-title]"></ng-content>
<label>
Email address
<input type="email" name="email" ngModel>
</label>
<label>
Password
<input type="password" name="password" ngModel>
</label>
<ng-content select="[auth-form-submit]"></ng-content>
</form>
</div>

ng-content also accept customer component.

For example, there is a component:

@Component({
selector: 'auth-remember',
template: `
<label>
<input type="checkbox" (change)="onChecked($event.target.checked)">
Keep me logged in
</label>
`
})
export class AuthRememberComponent { @Output() checked: EventEmitter<boolean> = new EventEmitter<boolean>(); onChecked(value: boolean) {
this.checked.emit(value);
}
}

And we can use it:

      <auth-form
(submitted)="loginUser($event)">
<h3>Login</h3>
<auth-remember
(checked)="rememberUser($event)">
</auth-remember>
<button type="submit">
Login
</button>
</auth-form>

Insie form component, we add slot for the new component.

    <div>
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select="h3"></ng-content>
<label>
Email address
<input type="email" name="email" ngModel>
</label>
<label>
Password
<input type="password" name="password" ngModel>
</label>
<ng-content select="auth-remember"></ng-content>
<ng-content select="button"></ng-content>
</form>
</div>

Lastly, just like 'switch' in any programming lanugage, it has a 'default' case, for content projection is the same, anything which is not match to the selector, it will goes to default slot. So how to define a default slot for content projection?

Actually it is quite símple:

<div>
<ng-content select=".higlight"></ng-content>
<ng-content select="authComponent"></ng-content>
<!-- Default case-->
<ng-content></ng-content>
</div>

[Angular] Content Projection with ng-content的更多相关文章

  1. [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 ...

  2. [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 ...

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

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

  4. iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析

    本篇博客的内容也不算太复杂,算是AutoLayout的一些高级的用法.本篇博客我们主要通过一些示例来看一下AutoLayout中的Content Hugging Priority以及Content C ...

  5. angular2 - content projection-

    angular2中的内容映射: App.component: <my-day> <my-lucky> </my-lucky> </my-day> MyD ...

  6. AutoLayout学习之理解intrinsicContentSize,Content Hugging Priority,Content Compression Resistance Priority

    TableViewCell的高度计算应该是所有开发者都会使用到的东西,之前都是用代码计算的方法来计算这个高度.最近有时间看了几个计算Cell高度的方法.基本上都用到了AutoLayout,这篇首先介绍 ...

  7. HTML连载53-网易注册界面实战之content的头部、content注册信息

    一. 这次完成了content部分的右边图片以及content的top部分的边角填充 <!DOCTYPE html> <html lang="en"> &l ...

  8. angular报错:angular.min.js:118Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq

    报错代码如下: <div ng-controller="HelloAngular"> <p>{{greeting.text}},angular</p& ...

  9. Angular CLI 启动 版本ng 4

    npm install -g angular-cli ng -v ng new project_name cd project_name ng serve 浏览器打开输入 localhost:4200

随机推荐

  1. python投票统计程序,统计序列中各个数值的份数,字典的应用。

    这里可能会用到字典的知识, 字典主要是用来存储最后的统计结果. 字典的用法:http://www.runoob.com/python/python-dictionary.html https://ww ...

  2. v-for实现循环嵌套

    <!DOCTYPE html> <html lang="en"> <head> <title></title> < ...

  3. 利用ServiceWorker实现页面的快速加载和离线访问

    Service workers 本质上充当Web应用程序与浏览器之间的代理服务器,也可以在网络可用时作为浏览器和网络间的代理.它们旨在(除其他之外)使得能够创建有效的离线体验,拦截网络请求并基于网络是 ...

  4. CodeVs——T 4919 线段树练习4

    http://codevs.cn/problem/4919/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  5. android问题及其解决-优化listView卡顿和怎样禁用ListView的fling

    问题解决-优化listView卡顿和怎样禁用ListView的fling 前戏非常长,转载请保留出处:http://blog.csdn.net/u012123160/article/details/4 ...

  6. the steps that may be taken to solve a feature selection problem:特征选择的步骤

    參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...

  7. Testin云測与ARM 战略合作:推动全球移动应用加速进入中国市场

    Testin云測与ARM 战略合作:推动全球移动应用加速进入中国市场 2014/10/14 · Testin · 业界资讯 (中国北京–2014年10月14日 )全球最大的移动游戏.应用真机和用户云測 ...

  8. 怎样让IE支持自己定义协议

    浏览QQ空间的时候发现,仅仅要在IE地址中输入象一下这样的形式的地址. tencent://Message/?Uin=251464630&websiteName=qzone.qq.com&am ...

  9. android studio 一次编译错误:Error:Minimum supported Gradle version is 2.14.1.

    因为需要,今天从git上重新下载工程到另一个目录下,结果运行的时候报了这个错:Error:Minimum supported Gradle version is 2.14.1.  Current ve ...

  10. Node.js自学笔记之回调函数

    写在前面:如果你是一个前端程序员,你不懂得像PHP.Python或Ruby等动态编程语言,然后你想创建自己的服务,那么Node.js是一个非常好的选择.这段时间对node.js进行了简单的学习,在这里 ...