angular4 一些技巧 ng-content ng-template

主要内容:

  1. ng-content
  2. ng-template
  3. *ngTemplateOutlet
  4. ng-container

1, 2 原文链接 Angular2 Tips & Tricks

3, 4 原文链接 Angular ng-template, ng-container and ngTemplateOutlet - The Complete Guide To Angular Templates

1. Content Projection (内容投影)

Content Projection in Angular with ng-content 这篇文章讲的比较详细

使用 <ng-content></ng-content> 可以实现内容投影,这个相当于angular1.x中的 transclude。这个还有一个 select 属性,用来选择投影的内容。

内容投影就是在组件内部有1个或者多个槽点(slots)。多用于Sections或Dialogs, 外部样式固定,内部包含实际的内容。

// section.component.ts
@Component({
  selector: 'app-section',
  templateUrl: './section.component.html',
  styleUrls: ['./section.component.css']
})
export class SectionComponent implements OnInit {
  private visible: boolean = true; // 用来控制显示或隐藏
  constructor() { }
  ngOnInit() {
  }
}
// section.component.html
<div>
  <h1>
    <a href="#" (click)="visible = !visible">
      <ng-content select="header"></ng-content> // 此处有个 'select' 属性, 选择 'header'
    </a>
  </h1>
  <section *ngIf="visible">
    <ng-content></ng-content>  // 这里也有一个 ng-content
  </section>
</div>

app.component中

// app.component.ts
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
}

// app.component.html
<div>
    <app-section> // 内容投影
        <header>hello</header>
        <p>someone like you</p>
    </app-section>
</div>

最后显示效果如下

ng-content.png

可以看出 有 select 属性的就只显示选择的内容, 没有改属性的会显示剩下的内容,而不是全部都显示

ng-template 模板插件(template outlet)

ngTemplateOutlet 接受一个 模板引用 和一个上下文对象的模板插件,相当于Angular1.x中的 ng-included.也可以类比 router-outlet,相当于 其它模版的入口

// section.component.ts
import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-section',
  templateUrl: './section.component.html'
})
export class SectionComponent implements OnInit {
  @Input() name: string; // 父组件传入的上下文
  @Input() template: any; // 父组件传入的模板名称
  
  constructor() { }
  ngOnInit() {
  }
}

// section.component.html
<div>
  <h1>Welcome</h1>
  <ng-template // 这里插入其他的模板内容
    [ngTemplateOutlet]="template" // 模板名
    [ngOutletContext]="{ name: name }" // 上下文对象
  >
  </ng-template>
</div>

父组件

// app.component.html
<div>
   // 这个模板将被放在 子组件 <app-section>中
    <ng-template let-name="name"  #myTemplate> // let-name="name"为下上文名  '#myTemplate'问模板名
        hi <strong>{{ name }}</strong> on {{ data.toLocaleString() }}
    </ng-template>

    <app-section 
        [name]="customer"        // 上下文名
        [template]="myTemplate"  // 模板名
    ></app-section>
</div>

// app.component.ts
import { Component, Input, ViewChild } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  @ViewChild('myTemplate') myTemplate: any;

  data: Date = new Date();
  customer: string = 'James';
}

可以看出父组件中,对要插入的模板可以使用 let-name=name 创建模板的上下文对象名, #myTemplate 用来创建模板名。

子组件中需要引入动态的模板,可以使用 [ngTemplateOutlet]="outerTemplate" 来表示引入的外部模板名, 使用 [ngOutletContext]="{name: name}" 来表示下上文对象

最后显示效果:

ng-template.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • html5中的template template标签的含义:HTML <template>元素是一种用于保存客户端...
    OnePiece索隆阅读 31,716评论 2 9
  • 组件基础 组件用来包装特定的功能,应用程序的有序运行依赖于组件之间的协同工作。组件是angular应用的最小逻辑单...
    oWSQo阅读 5,218评论 0 0
  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 5,581评论 0 3
  • Angular应用程序启动 ng-app指令 指令指定Angular应用程序的根(root)元素,用于启动Angu...
    angelwgh阅读 3,308评论 0 1
  • (一) OBJC 面向对象的三大特征 封装:把具体的对象封装成抽象的类,隐藏内部实现,对象的属性和实现细节,仅对...
    牧马人_hlc阅读 3,388评论 0 6

友情链接更多精彩内容