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>

最新文章

  1. SQL Server性能计数器收集汇总方案(Reporting Service)
  2. 设计模式--工厂模式Factory(创建型)
  3. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
  4. java.lang.ArrayIndexOutOfBoundsException: 1
  5. [No000055]教你早晨清肠、除口臭、色斑、大肚腩
  6. cd hit使用
  7. Android优秀资源整理合集(论菜鸟到高级攻城狮)
  8. 第二百六十九天 how can I 坚持
  9. JNDI Tutorial
  10. Spring整合Shiro做权限控制模块详细案例分析
  11. 0122——UITabBarController
  12. CSS实现导航条Tab的三种方法
  13. javascript open window
  14. 变量 || 基本数据类型 || if、while语句
  15. Node.js开发工具、开发包、框架等总结
  16. Node.js 安装配置介绍
  17. GitHub下载克隆clone指定的分支tag代码
  18. 深入java----java内存区域及对象的创建
  19. SpringCloud-day03-服务注册与发现组件Eureka
  20. $(&quot;&quot;).append无反应

热门文章

  1. 机器学习分支:active learning、incremental learning、online machine learning
  2. 驱动学习3-make
  3. 【AtCoder Beginner Contest 074 C】Sugar Water
  4. (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text
  5. 9.6 Binder系统_驱动情景分析_server的多线程实现
  6. 【习题5-5 UVA-10391】Compound Words
  7. Android中各种drawable的使用
  8. IGeometryCollection Interface
  9. Delphi的指针(有图,很清楚)
  10. jquery-12 jquery的ajax如何使用