Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NgTemplateOutlet #74

Open
deepthan opened this issue Jul 27, 2020 · 0 comments
Open

NgTemplateOutlet #74

deepthan opened this issue Jul 27, 2020 · 0 comments

Comments

@deepthan
Copy link
Owner

NgTemplateOutlet

官网介绍 | 源码

代码示例

<ng-container *ngTemplateOutlet="eng; context: {$implicit: 'World', detail: '我来了!'}"></ng-container>

<ng-template #eng let-data let-detail="detail"><span>Hello {{data}}! {{ detail }}</span></ng-template>

作用

将一个ng-template包裹的模板插入到宿主元素中。

参数

  • ngTemplateOutlet: TemplateRef | null
    第一个参数是需要传入的模板

  • ngTemplateOutletContext: Object | null
    第二个参数是需要传入的数据。形式如

 {$implicit: 'World', detail: '我来了!'}

它是一个对象格式上下文的数据,$implicit的数据可以通过let-data获取到,data是用户可以自定义的名称。其他的数据需要通过let-xxx="xxx"来获取,如detail是通过let-detail="detail", {{ detail }}

一块内容如何在代码中使用两次?

<ng-container *ngIf="show">
  <ng-container *ngTemplateOutlet="contentTpl"></ng-container>
  deepthan
</ng-container>

<ng-container *ngIf="!show">
  <ng-container *ngTemplateOutlet="contentTpl"></ng-container>
  momomo
</ng-container>

<ng-template #contentTpl>你好,</ng-template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant