Angular利用內(nèi)容投射向組件輸入ngForOf模板的方法
現(xiàn)在,我們寫(xiě)一個(gè)組件puppiesListCmp,用于顯示小狗狗的列表:
//puppies-list.component.ts
@Component({
selector: 'puppies-list',
template: `
<div *ngFor="let puppy of puppies">
<span>{{puppy.name}}</span>
<span>{{puppy.age}}</span>
<span>{{puppy.color}}</span>
</div>
`
})
export class puppiesListCmp{
@Input() puppies: Puppy[];
}
interface Puppy {
name: string,
age: number,
color: string
}
然后這樣使用:
//app.component.ts
@Component({
selector: 'my-app',
template: `
<puppies-list [puppies]="puppies"></puppies-list>
`
})
export class App{
puppies = [
{
name: "sam",
age: 0.6,
color: "yellow"
},
{
name: "bingo",
age: 1.5,
color: "black"
}
]
}
效果就行這樣:

但是,我希望我們的puppiesListCmp組件可以滿(mǎn)足不同的需求,比如在數(shù)據(jù)不變的情況下只顯示小狗狗的name和color,就像這樣:

這就是本文的重點(diǎn)了。我們需要實(shí)現(xiàn)用戶(hù)自定義模板!
現(xiàn)在我們不寫(xiě)死組件的模板了,而是讓用戶(hù)從外部輸入!
首先,我們的組件模板:
<div *ngFor="let puppy of puppies">
<span>{{puppy.name}}</span>
<span>{{puppy.age}}</span>
<span>{{puppy.color}}</span>
</div>
等價(jià)于:
<ng-template ngFor let-puppy [ngForOf]="puppies">
<div>
<span>{{puppy.name}}</span>
<span>{{puppy.age}}</span>
<span>{{puppy.color}}</span>
</div>
</ng-template>
然后,用@ContentChild(關(guān)于@ContentChild可以查看這里,需FQ)獲取到外部(相對(duì)puppiesListCmp組件而言)自定義模板,并賦給ngForTemplate。也就是說(shuō),這部分:
<div>
<span>{{puppy.name}}</span>
<span>{{puppy.age}}</span>
<span>{{puppy.color}}</span>
</div>
不再像之前那樣寫(xiě)死在組件里了,而是由使用者在父組件中自定義,然后利用Angular的內(nèi)容投射(Content Projection),投射到puppiesListCmp組件里面。就像這樣:
//puppies-list.component.ts
import { Component, Input, ContentChild, TemplateRef } from '@angular/core';
import { NgForOfContext } from '@angular/common';
@Component({
selector: 'puppies-list',
template: `
<ng-template ngFor let-puppy [ngForOf]="puppies" [ngForTemplate]="tpl"></ng-template>
`
})
export class puppiesListCmp{
@Input() puppies: Puppy[];
@ContentChild(TemplateRef) tpl: TemplateRef<NgForOfContext<Puppy>>
}
interface Puppy {
name: string,
age: number,
color: string
}
這樣我們的組件就算完成了。然后我們使用它:
//app.component.ts
@Component({
selector: 'my-app',
template: `
<puppies-list [puppies]="puppies">
<ng-template let-puppy>
<div>
<span>{{puppy.name}}</span>
<span>{{puppy.age}}</span>
<span>{{puppy.color}}</span>
</div>
</ng-template>
</puppies-list>
`
})
效果還是一樣的:

如果我們只要顯示小狗狗的name和color,只要這樣寫(xiě)就好了:
//app.component.ts
@Component({
selector: 'my-app',
template: `
<puppies-list [puppies]="puppies">
<ng-template let-puppy>
<div>
<span>{{puppy.name}}</span>
<span>{{puppy.color}}</span>
</div>
</ng-template>
</puppies-list>
`
})
效果就像這樣:

這樣的組件很靈活,想要什么樣的效果都可以定制,這就實(shí)現(xiàn)了組件的復(fù)用。
好了,本文就到此為止了。不當(dāng)之處,歡迎指出!希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解
這篇文章主要介紹了AngularJS基于factory創(chuàng)建自定義服務(wù)的方法,結(jié)合實(shí)例形式分析了AngularJS使用factory創(chuàng)建自定義服務(wù)的具體步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
AngularJS變量及過(guò)濾器Filter用法分析
這篇文章主要介紹了AngularJS變量及過(guò)濾器Filter用法,結(jié)合實(shí)例形式分析了AngularJS變量、過(guò)濾器及自定義過(guò)濾器的相關(guān)用法與注意事項(xiàng),需要的朋友可以參考下2016-11-11
AngularJS前端頁(yè)面操作之用戶(hù)修改密碼功能示例
這篇文章主要介紹了AngularJS前端頁(yè)面操作之用戶(hù)修改密碼功能,結(jié)合具體實(shí)例形式分析了AngularJS針對(duì)前端用戶(hù)修改密碼的判斷操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03
Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果的示例代碼
這篇文章主要介紹了Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過(guò)濾字符查找功能
這篇文章主要介紹了 Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過(guò)濾字符查找功能,代碼簡(jiǎn)單易懂,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07
Angular實(shí)現(xiàn)的table表格排序功能完整示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的table表格排序功能,結(jié)合完整實(shí)例形式分析了AngularJS表格排序所涉及的事件響應(yīng)、元素遍歷、屬性修改等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
AngularJS實(shí)現(xiàn)用戶(hù)登錄狀態(tài)判斷的方法(Model添加攔截過(guò)濾器,路由增加限制)
這篇文章主要介紹了AngularJS實(shí)現(xiàn)用戶(hù)登錄狀態(tài)判斷的方法,通過(guò)Model添加攔截過(guò)濾器,路由增加限制實(shí)現(xiàn)針對(duì)登陸狀態(tài)的判斷功能,需要的朋友可以參考下2016-12-12
AngularJS 實(shí)現(xiàn)購(gòu)物車(chē)全選反選功能
這篇文章主要介紹了AngularJS 實(shí)現(xiàn)購(gòu)物車(chē)全選反選功能,需要的朋友可以參考下2017-10-10

