Angular ng-content元素的学习笔记
ng-content 的select 属性其实和 css 选择器的工作方式类似,用于帮助 Angular 找到投影内容在 DOM 中的正确放置位置。
我个人把ng-content理解成Component工厂,能根据用户指定的配置信息,动态生成Component,即configurable Component,这种行为称之为Content Projection.
They are used to create configurable components. This means the components can be configured depending on the needs of its user. This is well known as Content Projection. Components that are used in published libraries make use of ng-content to make themselves configurable.
看一个例子:
project-content footer区域的div里定义了ng-content.
客户如何将自己的UI内容注入到project-content中?
运行时,上图的div元素,会被渲染到project-content Component的ng-content标签内。
看一个更复杂一些的例子:
ng-content的select属性值,决定了具体哪些UI内容会被注入到ng-content里。
Here we have first select to render header h1 element. If the projected content has no h1 element it won’t render anything.
第一个select会渲染h1元素,如果被注入的内容中并没有h1元素,则Angular不会进行内容注入。
Similarly the second select looks for a div.
The rest of the content gets rendered inside the last ng-contentwith no select.
剩下的所有内容都被注入到最后一个不含select属性的ng-content中去。
消费该project-content的例子:
更多Jerry的原创文章,尽在:“汪子熙”:
相关文章
- [Angular Directive] 2. Add Inputs to Angular 2 Directives
- [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled
- Angular开发小笔记
- [Angular] Component's dependency injection
- Angular CLI builder 学习笔记
- Angular应用ng serve命令行的学习笔记
- Angular 里的 Service Worker
- Angular Universal 学习笔记
- Angular Form (响应式Form) 学习笔记
- Angular CLI builder 学习笔记
- Angular 依赖注入学习笔记之工厂函数的用法
- Angular Universal 学习笔记 - 客户端渲染和服务器端渲染的区别
- Angular 服务器端渲染的学习笔记(一)
- Angular Jasmine 里一些常用概念学习笔记 - describe, it, beforeEach的用法
- Angular zone学习笔记
- 使用Angular的property binding给HTML DOM元素的class动态赋值
- Angular form 官网文档的学习笔记:Angular两种实现form的方式
- Angular 依赖注入的学习笔记
- Angular Component的DOM级别的单元测试方法
- angular基本知识学习笔记 - NgModule的基本概念
- angular基本知识学习笔记 - Component的基本概念
- angular学习(二):Controller定义总结