angular4 一些技巧 ng-content ng-template

主要內(nèi)容:

  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 (內(nèi)容投影)

Content Projection in Angular with ng-content 這篇文章講的比較詳細(xì)

使用 <ng-content></ng-content> 可以實現(xiàn)內(nèi)容投影,這個相當(dāng)于angular1.x中的 transclude。這個還有一個 select 屬性馏锡,用來選擇投影的內(nèi)容哥桥。

內(nèi)容投影就是在組件內(nèi)部有1個或者多個槽點(slots)耍休。多用于Sections或Dialogs, 外部樣式固定蜓陌,內(nèi)部包含實際的內(nèi)容徐许。

// 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> // 內(nèi)容投影
        <header>hello</header>
        <p>someone like you</p>
    </app-section>
</div>

最后顯示效果如下

ng-content.png

可以看出 有 select 屬性的就只顯示選擇的內(nèi)容丝里, 沒有改屬性的會顯示剩下的內(nèi)容曲初,而不是全部都顯示

ng-template 模板插件(template outlet)

ngTemplateOutlet 接受一個 模板引用 和一個上下文對象的模板插件,相當(dāng)于Angular1.x中的 ng-included.也可以類比 router-outlet,相當(dāng)于 其它模版的入口

// 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 // 這里插入其他的模板內(nèi)容
    [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 創(chuàng)建模板的上下文對象名臼婆, #myTemplate 用來創(chuàng)建模板名。

子組件中需要引入動態(tài)的模板幌绍,可以使用 [ngTemplateOutlet]="outerTemplate" 來表示引入的外部模板名颁褂, 使用 [ngOutletContext]="{name: name}" 來表示下上文對象

最后顯示效果:

ng-template.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市傀广,隨后出現(xiàn)的幾起案子颁独,更是在濱河造成了極大的恐慌,老刑警劉巖伪冰,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件誓酒,死亡現(xiàn)場離奇詭異,居然都是意外死亡贮聂,警方通過查閱死者的電腦和手機(jī)丰捷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進(jìn)店門坯墨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人病往,你說我怎么就攤上這事捣染。” “怎么了停巷?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵耍攘,是天一觀的道長。 經(jīng)常有香客問我畔勤,道長蕾各,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任庆揪,我火速辦了婚禮式曲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘缸榛。我一直安慰自己吝羞,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布内颗。 她就那樣靜靜地躺著钧排,像睡著了一般。 火紅的嫁衣襯著肌膚如雪均澳。 梳的紋絲不亂的頭發(fā)上恨溜,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機(jī)與錄音找前,去河邊找鬼糟袁。 笑死,一個胖子當(dāng)著我的面吹牛躺盛,可吹牛的內(nèi)容都是我干的系吭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼颗品,長吁一口氣:“原來是場噩夢啊……” “哼肯尺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起躯枢,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤则吟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锄蹂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體氓仲,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了敬扛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晰洒。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖啥箭,靈堂內(nèi)的尸體忽然破棺而出谍珊,到底是詐尸還是另有隱情,我是刑警寧澤急侥,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布砌滞,位于F島的核電站,受9級特大地震影響坏怪,放射性物質(zhì)發(fā)生泄漏贝润。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一铝宵、第九天 我趴在偏房一處隱蔽的房頂上張望打掘。 院中可真熱鬧,春花似錦鹏秋、人聲如沸尊蚁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枝誊。三九已至况芒,卻和暖如春惜纸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背绝骚。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工耐版, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人压汪。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓粪牲,卻偏偏與公主長得像,于是被迫代替她去往敵國和親止剖。 傳聞我的和親對象是個殘疾皇子腺阳,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

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

  • html5中的template template標(biāo)簽的含義:HTML <template>元素是一種用于保存客戶端...
    OnePiece索隆閱讀 31,555評論 2 9
  • 組件基礎(chǔ) 組件用來包裝特定的功能,應(yīng)用程序的有序運行依賴于組件之間的協(xié)同工作穿香。組件是angular應(yīng)用的最小邏輯單...
    oWSQo閱讀 1,374評論 0 0
  • AngularJS是什么亭引?AngularJs(后面就簡稱ng了)是一個用于設(shè)計動態(tài)web應(yīng)用的結(jié)構(gòu)框架。首先皮获,它是...
    200813閱讀 1,611評論 0 3
  • Angular應(yīng)用程序啟動 ng-app指令 指令指定Angular應(yīng)用程序的根(root)元素焙蚓,用于啟動Angu...
    angelwgh閱讀 474評論 0 1
  • (一) OBJC 面向?qū)ο蟮娜筇卣?封裝:把具體的對象封裝成抽象的類,隱藏內(nèi)部實現(xiàn),對象的屬性和實現(xiàn)細(xì)節(jié)购公,僅對...
    牧馬人_hlc閱讀 490評論 0 6