angular2-chapter08

Reusing Components with Content Projection

Check code at: https://github.com/wghglory/angular2-fundamental

Content Projection

Now in event-detail page, there's a list of sessions for that event. We want to collapse and expand every session when clicking the session div. Since this feature is quite common, and probably get reused somewhere else, we need to put this function into common folder.

  1. In session-list.component.html
<div class="row" *ngFor="let session of sessions">
    <div class="col-md-10">
        <!-- content projection -->
        <collapsible-well [title]="session.name">
            <h6>{{session.presenter}}</h6>
            <span>Duration: {{session.duration}}</span><br />
            <span>Level: {{session.level}}</span>
            <p>{{session.abstract}}</p>
        </collapsible-well>
        <!-- <div class="well">
            <h4>{{session.name}}</h4>
            <h6>{{session.presenter}}</h6>
            <span>Duration: {{session.duration}}</span><br />
            <span>Level: {{session.level}}</span>
            <p>{{session.abstract}}</p>
        </div> -->
    </div>
</div>
  1. create common/collapsible-well.component, and register it in module

Note: <ng-content> is responsible to show html inside its selector "collapsible-well": h6+span+span+p in this case

import { Component, Input } from '@angular/core'

@Component({
    selector: 'collapsible-well',
    template: `
        <div (click)="toggleContent()" class="well pointable">
            <h4 class="well-title">{{title}}</h4>
            <ng-content *ngIf="visible"></ng-content>
        </div>
    `
})
export class CollapsibleWellComponent {
    @Input() title: string;
    visible: boolean = true;

    toggleContent() {
        this.visible = !this.visible;
    }
}

Multiple Slot Content Projection

We also want to add a flame icon next to the session title if any session is hot. If we add this feature into collapsible-well template after <h4 class="well-title">{{title}}</h4>, this component is not generic.

session-list.component.html:

well-title and well-body attributes can be used as selectors in collapsible-well.component template. Attribute is better than class since it won't give us any conflict for a css class.

<div class="row" *ngFor="let session of sessions">
    <div class="col-md-10">

        <!-- multi slot content projection -->
        <collapsible-well>
            <div well-title>
                {{session.name}}
                <i *ngIf="session.voters.length>3" class="glyphicon glyphicon-fire" style="color:red;"></i>
            </div>

            <div well-body>
                <h6>{{session.presenter}}</h6>
                <span>Duration: {{session.duration}}</span><br />
                <span>Level: {{session.level}}</span>
                <p>{{session.abstract}}</p>
            </div>
        </collapsible-well>

        <!-- content projection -->
        <!-- <collapsible-well [title]="session.name">
            <h6>{{session.presenter}}</h6>
            <span>Duration: {{session.duration}}</span><br />
            <span>Level: {{session.level}}</span>
            <p>{{session.abstract}}</p>
        </collapsible-well> -->

        <!-- <div class="well">
            <h4>{{session.name}}</h4>
            <h6>{{session.presenter}}</h6>
            <span>Duration: {{session.duration}}</span><br />
            <span>Level: {{session.level}}</span>
            <p>{{session.abstract}}</p>
        </div> -->
    </div>
</div>

collapsible-well.component.ts updates template:

Note: ng-content with select is used to display the html. "select" can have id selector, class selector, etc. Attribute selector is best for multiple slot content projection

import { Component, Input } from '@angular/core'

@Component({
    selector: 'collapsible-well',
    template: `
        <div (click)="toggleContent()" class="well pointable">
            <h4>
                <ng-content select="[well-title]"></ng-content>
            </h4>
            <ng-content *ngIf="visible" select="[well-body]"></ng-content>
        </div>
    `
})
export class CollapsibleWellComponent {
    @Input() title: string;
    visible: boolean = true;

    toggleContent() {
        this.visible = !this.visible;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酵熙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌姻氨,老刑警劉巖蕴坪,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厌均,死亡現(xiàn)場離奇詭異伴郁,居然都是意外死亡沐序,警方通過查閱死者的電腦和手機邑茄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門姨蝴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人撩扒,你說我怎么就攤上這事似扔。” “怎么了搓谆?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵炒辉,是天一觀的道長。 經(jīng)常有香客問我泉手,道長黔寇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任斩萌,我火速辦了婚禮缝裤,結果婚禮上,老公的妹妹穿的比我還像新娘颊郎。我一直安慰自己憋飞,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布姆吭。 她就那樣靜靜地躺著榛做,像睡著了一般。 火紅的嫁衣襯著肌膚如雪内狸。 梳的紋絲不亂的頭發(fā)上检眯,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音昆淡,去河邊找鬼锰瘸。 笑死,一個胖子當著我的面吹牛昂灵,可吹牛的內(nèi)容都是我干的避凝。 我是一名探鬼主播舞萄,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼恕曲!你這毒婦竟也來了鹏氧?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤佩谣,失蹤者是張志新(化名)和其女友劉穎把还,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體茸俭,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡吊履,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了调鬓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片艇炎。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖腾窝,靈堂內(nèi)的尸體忽然破棺而出缀踪,到底是詐尸還是另有隱情,我是刑警寧澤虹脯,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布驴娃,位于F島的核電站,受9級特大地震影響循集,放射性物質(zhì)發(fā)生泄漏唇敞。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一咒彤、第九天 我趴在偏房一處隱蔽的房頂上張望疆柔。 院中可真熱鬧,春花似錦镶柱、人聲如沸旷档。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鞋屈。三九已至,卻和暖如春查吊,著一層夾襖步出監(jiān)牢的瞬間谐区,已是汗流浹背湖蜕。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工逻卖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人昭抒。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓评也,卻偏偏與公主長得像炼杖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子盗迟,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 今天和老師溝通了孩子期末考試的情況坤邪。語文高分排名前三,很棒~數(shù)學滑鐵盧罚缕,跌到平均分以下艇纺,英語也低于平均分。感謝老師...
    爬山虎7544閱讀 357評論 0 0
  • 現(xiàn)在p圖軟件越來越智能邮弹,p圖越來越方便黔衡,效果越來越好蔬捷,照片p的越來越美晓褪,“照片”因此也變成“照騙”了。即使沒有任何...
    q肥有大智慧閱讀 740評論 2 0
  • 近期目標是財富增長商架!通過學習智慧和實踐智慧并用成功的案例傳播智慧与纽,讓身邊更多的人接受智慧并學習智慧侣签。讓世界因智慧而...
    belivePossible閱讀 122評論 0 1