您好,登錄后才能下訂單哦!
這篇文章主要講解了“angular內容投影怎么實現”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“angular內容投影怎么實現”吧!
利用ng-content來實現
<!-- 組件 - app-content-single --> <div> <h3>標題</h3> <!-- 投影內容顯示位置 --> <ng-content></ng-content> </div> <!-- 使用 --> <app-content-single> <div>this is content</div> </app-content-single>
利用ng-content
來實現
<!-- 組件 - app-content-more --> <div> <h4>Herder Title</h4> <ng-content select=".header"></ng-content> <h4>Body Title</h4> <ng-content select="[body]"></ng-content> <h4>Default Title</h4> <ng-content></ng-content> <h4>Footer Title</h4> <ng-content select="footer"></ng-content> </div> <!-- 使用 --> <app-content-more> <div>this is default01</div> <div class="header">this is header</div> <div>this is default02</div> <div body>this is body</div> <div>this is default03</div> <footer>this is footer</footer> <div>this is default04</div> </app-content-more>
有條件的內容投影-ng-template
, ng-container
, directive
等來配合實現
eg: 假設現在有一個人員列表,當某個人的money大于200的時候,額外添加組件中模板定義的內容
定義一個 appChildRef 指令來配合 ng-template 獲取模板
import { Directive, TemplateRef } from '@angular/core'; @Directive({ selector: '[appChildRef]' }) export class ChildRefDirective { constructor(public templateRef: TemplateRef<any>) { } }
<div class="list-item" *ngFor="let person of persons;"> <div>Name: {{ person.name }}</div> <div>Money: {{ person.money }}</div> <div *ngIf="person.money > 200"> <ng-container *ngIf="childRef" [ngTemplateOutlet]="childRef.templateRef"></ng-container> </div> </div>
import { Component, ContentChild, OnInit } from '@angular/core'; import { ChildRefDirective } from '../../../../directives/child-ref.directive'; @Component({ selector: 'app-persons', templateUrl: './persons.component.html', styleUrls: ['./persons.component.scss'] }) export class PersonsComponent implements OnInit { persons: { name: string; money: number; }[] = [ { name: '杰克', money: 120 }, { name: '李莉', money: 210 }, { name: '張三', money: 170 }, ]; @ContentChild(ChildRefDirective, { static: true }) childRef!: ChildRefDirective; constructor() { } ngOnInit(): void { } }
<app-persons> <ng-template appChildRef> <div >this is child ref content</div> </ng-template> </app-persons>
eg: 現在希望通過 persons 數據中的字段進行綁定內嵌的模板來顯示
import { Directive, Input, TemplateRef } from '@angular/core'; @Directive({ selector: '[appChildRef]' }) export class ChildRefDirective { // 接受定義模板名稱-通過這個名稱和 persons 中的render字段對應進行顯示對應的模板內容 @Input() appChildRef!: string; constructor(public templateRef: TemplateRef<any>) { } }
<div class="list-item" *ngFor="let person of persons;let i=index;"> <div>Name: {{ person.name }}</div> <div>Money: {{ person.money }}</div> <!-- <div *ngIf="person.money > 200"> <ng-container *ngIf="childRef" [ngTemplateOutlet]="childRef.templateRef"></ng-container> </div> --> <div *ngIf="person.render && tempRefs[person.render]"> <!-- 配合 ngTemplateOutlet 指令給template傳遞當前person的數據 --> <ng-container *ngTemplateOutlet="tempRefs[person.render].templateRef; context: { $implicit: person, i: i }"></ng-container> </div> </div>
import { Component, ContentChild, ContentChildren, OnInit, QueryList } from '@angular/core'; import { ChildRefDirective } from '../../../../directives/child-ref.directive'; @Component({ selector: 'app-form-unit', templateUrl: './form-unit.component.html', styleUrls: ['./form-unit.component.scss'] }) export class FormUnitComponent implements OnInit { persons: { name: string; money: number; render?: string; }[] = [ { name: '杰克', money: 120, render: 'temp1' }, { name: '李莉', money: 210, render: 'temp2' }, { name: '張三', money: 170, render: 'temp3' }, ]; // @ContentChild(ChildRefDirective, { static: true }) childRef!: ChildRefDirective; @ContentChildren(ChildRefDirective) childrenRef!: QueryList<ChildRefDirective>; get tempRefs() { const aObj: any = {}; this.childrenRef.forEach(template => { const key: string = template.appChildRef; aObj[key] = template; }) return aObj; } constructor() { } ngOnInit(): void { } }
<app-persons> <ng-template appChildRef="temp1" let-person let-index="i"> <div >{{index}}-{{person.name}}: this is temp1</div> </ng-template> <ng-template appChildRef="temp2" let-person let-index="i"> <div >{{index}}-{{person.name}}: this is temp2</div> </ng-template> <ng-template appChildRef="temp3" let-person let-index="i"> <div >{{index}}-{{person.name}}: this is temp3</div> </ng-template> </app-persons>
感謝各位的閱讀,以上就是“angular內容投影怎么實現”的內容了,經過本文的學習后,相信大家對angular內容投影怎么實現這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。