然后你发现了 <ng-content>,并找到了一些关于它的文章,进而实现了所需的功能。 接下来我们来通过一个简单的示例,一步步介绍 <ng-content> 所涉及的内容。 如果 ng-content 上没有设置 select 属性,它将接收全部内容,或接收不匹配任何其他 ng-content 元素的内容。 ></ng-content>
-- single slot transclusion here --> <ng-content></ng-content>
这时就要请出本文的主角 ng-content。 ></ng-content>
select="h3"></ng-content> <label> 邮箱 <input type="email" name="email" ngModel select="auth-remember"></ng-content> <auth-message [style.display]="(showMessage 'inherit' : 'none')"> </auth-message> <ng-content select="button"></ng-content> select="auth-remember"></ng-content> <auth-message [style.display]="(showMessage 'inherit' : 'none')"> </auth-message> <ng-content select="button"></ng-content>
接下来我们来看一下,如何利用 <ng-content> 指令实现上述的功能。 如果 ng-content 上没有设置 select 属性,它将接收全部内容,或接收不匹配任何其他 ng-content 元素的内容。 select="auth-remember"></ng-content> <ng-content select="button"></ng-content> </form> ContentChild 上面我们已经介绍了内容投射的相关概念及 <ng-content> 指令的常见用法。下面我们来介绍在组件内部,如何获取 <ng-content> 投射的内容。 /div> <ng-content select="button"></ng-content> </form>
投影一块内容 容器组件这样写
dialog-content">
toggle>
index-list">
this.count++; this.countChange.emit(this.count); } } ---- app/counter.component.html
<ng-content ></ng-content> Count: {{ count }} - <button (click)="increment()">Increment</button>
<以下是 v18 的一些亮点:指定 ng-content 的回退内容我们遇到的最受好评的问题之一是为 ng-content 指定默认内容。在 v18 中,它现在可用! 下面是一个简单示例:@Component({ selector: 'app-profile', template: ` <ng-content select=".greeting">Hello </ng-content> <ng-content>Unknown user</ng-content> `,})export class Profile {}现在我们可以使用组件:<app-profile
6. ng-content指令? 在标记之间可以添加内容,eg:
this is a paragraph
.那我们如果想在angular的标签之间添加内容呢,例如在<app-test></app-test>,这种情况下就可以使用<ng-content >tag directive is used</ng-content> 7. router.navigate 的使用? ViewChildren 都支持 Type<any>|Function|string 类型的选择器 不同点 ContentChild 用来从通过 Content Projection 方式 (ng-contentafter_content_component.dart (template) template: '''
{{comment}}
''', <ng-content>标记是外部内容的占位符。 内容投影的指示标记是(a)组件元素标签之间的HTML和(b)组件模板中存在<ng-content>标签。 AfterContent挂钩 AfterContent挂钩与AfterView挂钩类似。ng-content: name - 标签区域中的自定义内容。 value - 值区域中的自定义内容。 description - 描述区域中的自定义内容。
Content代表内容,这些内容在组件渲染时已经存在于组件声明标签的内部,通常在组件内部以<ng-content>为占位符 View代表视图,视图代表组件本身的模板,代表组件本身的渲染逻辑 @ContentChildren
Component({ selector: 'my-fader', template: `
Can you see me?
现在可以在 Angular 18 中为 ng-content 指定默认的内容。这允许开发人员在他们的组件中提供回退内容。
, template: '''
路由相关的指令或者术语 <router-outlet> :路由占位符,可以理解为渲染路由组件的区域,一个组件只能一个无命名<router-outlet>,命名的可以多个 ng-content: 可以嵌套一个组件的内容在另外一个组件中
visible"> <ng-content></ng-content>