首頁 > web前端 > js教程 > 主體

一文了解angular中的3種內容投影(單插槽、多插槽、有條件)

青灯夜游
發布: 2021-10-14 10:43:00
轉載
3303 人瀏覽過

這篇文章帶大家了解下angular中的內容投影,介紹一下單插槽內容投影、多插槽內容投影、有條件的內容投影,希望對大家有幫助!

一文了解angular中的3種內容投影(單插槽、多插槽、有條件)

【相關教學推薦:《angular教學》】

單插槽內容投影


一文了解angular中的3種內容投影(單插槽、多插槽、有條件)

##單一插槽內容投影是指建立一個元件,你可以在其中投影一個元件。

zippy-basic.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-zippy-basic',
  template: `
  <h2>单插槽内容投影</h2>
  <ng-content></ng-content>
`
})
export class ZippyBasicComponent {}
登入後複製

有了
ng-content

元素,該元件的使用者現在可以將自己的訊息投影到該元件中。例如:app.component.html

<!-- 将 app-zippy-basic 元素包裹的全部内容投影到 zippy-basic 组件中去 -->
<app-zippy-basic>
  <p>单插槽内容投影:投影数据</p>
</app-zippy-basic>
登入後複製
效果如下:
  • ng-content 元素是佔位符,它不會創建真正的DOM 元素。
  • ng-content 的那些自訂屬性將被忽略。 多重插槽內容投影
  • #元件範本含有多個
  • ng-content
  • 標籤。 為了區分投影的內容可以投影到對應ng-content標籤,需要使用
  • ng-content
標籤上的
select

屬性作為識別。

select

屬性支援標籤名稱、屬性、CSS 類別和 :not 偽類別的任意組合。
一文了解angular中的3種內容投影(單插槽、多插槽、有條件)不加入

select
屬性的

ng-content標籤將作為預設插槽。所有為匹配的投影內容都會投影在該ng-content的位置。

zippy-multislot.component.ts
import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-zippy-multislot&#39;,
  template: `
  <h2>多插槽内容投影</h2>
  <ng-content></ng-content>
  <ng-content select="[question]"></ng-content>
`
})
export class ZippyMultislotComponent {}
登入後複製
app.component.html

<!-- 使用 question 属性的内容将投影到带有 `select=[question]` 属性的 ng-content 元素。 -->
<app-zippy-multislot>
  <p question style="color: hotpink;">
    带question属性的p元素
  </p>
  <p style="color: lightgreen">不带question属性的p元素-->匹配到不带select属性的ng-content</p>
  <p>不带question属性的p元素-->匹配到不带select属性的ng-content</p>
</app-zippy-multislot>
登入後複製
效果如下:

#在前面的範例中,只有第二個ng-content 元素定義了select 屬性。結果,第一個 ng-content 元素就會接收投影到元件中的任何其他內容。 #推薦使用ng-container標籤,因為該標籤不需要渲染真實的DOM 元素。 類型templateRefExp
有條件的內容投影
<ng-container *ngTemplateOutlet="templateRefExp; context: contextExp"></ng-container>
<!-- 等同 -->
<ng-container [ngTemplateOutlet]="templateRefExp" [ngTemplateOutletContext]="contextExp"></ng-container>
登入後複製
參數
說明
TemplateRef | null

一個字串,用於定義模板參考以及模板的上下文物件##contextExp #Object | null是一個對象,該物件的鍵名將可以在局部模板中使用let 宣告中進行綁定。在上下文物件中使用 $implicit 為鍵名時,將把它作為預設值。

ng-template

標籤的

#ID一文了解angular中的3種內容投影(單插槽、多插槽、有條件)會符合

templateRefExp

,將ng- template標籤的內容嵌入到指定的

ngTemplateOutlet
    中。
  • 範例一:
    <header>头部</header>
    <main>
    	<h3>内容:</h3>
    	<ng-container [ngTemplateOutlet]="greet"></ng-container>
    </main>
    <footer>底部</footer>
    
    <ng-template #greet>
    	<div>
    		<h4>hi!</h4>
    		<h4>hello my dear friend!</h4>
    	</div>
    </ng-template>
    登入後複製
    效果:
  • #ViewChild與ContentChild

#ContentChild:與內容子節點有關,

操作投影進來的內容

;

ViewChild

:與視圖子節點有關,操作自身的視圖內容;

ContentChild

#在上一個部分,我們透過內容投影,讓自訂的元件標籤能夠嵌入html標籤或自訂元件標籤,那麼它如何操作投影進來的內容呢?

首先創建兩個元件

/**** part-b.component.ts ****/
import { Component, OnInit,Output} from &#39;@angular/core&#39;;

@Component({
	selector: &#39;app-content-part-b&#39;,
	templateUrl: &#39;./part-b.component.html&#39;,
	styleUrls: [&#39;./part-b.component.scss&#39;]
})

export class PartBComponent implements OnInit {
	constructor() { }
	ngOnInit() { }
	
	public func():void{
		console.log("PartB");
	} 
}
登入後複製
/**** part-a.component.ts ****/
import { Component, OnInit, ContentChild } from &#39;@angular/core&#39;;
// 1、引入 part-b 组件
import { PartBComponent } from &#39;../part-b/part-b.component&#39;;

@Component({
	selector: &#39;app-content-part-a&#39;,
	templateUrl: &#39;./part-a.component.html&#39;,
	styleUrls: [&#39;./part-a.component.scss&#39;]
})

export class PartAComponent implements OnInit {
	// 2、获取投影
	@ContentChild(PartBComponent) PartB:PartBComponent
	constructor() { }
	ngOnInit() {}
	ngAfterContentInit(): void {
		// 3、调用 part-b 组件的 func() 方法
		this.PartB.func();
	}
	public func() {
		console.log(&#39;PartA&#39;)
	}
}
登入後複製
part-b

元件的內容投影到

part-a元件中

 <!-- content.component.html -->
<div>
	<div>Content</div>
	<div>
		<app-content-part-a>
		<!-- 投影在part-a组件中的内容 -->
			<h1>PartA--start</h1>
			<app-content-part-b></app-content-part-b>
			<span>PartA--end</span>
		</app-content-part-a>
	</div>
</div>
登入後複製
在元件的生命週期裡面,有一個鉤子
ngAfterContentInit()是與投影內容初始化有關,所以我們有關投影的內容操作盡量放在它初始化完成之後進行

ViewChild#上一部分的ContentChild

操作的時投影進來的內容,而
ViewChild

操作的是自身的視圖內容 給上一個部分的content.component.html修改如下:

 <!-- content.component.html -->
<div>
	<div>Content</div>
	<div>
	<!-- 在此处引用模板变量 #partA -->
		<app-content-part-a #partA>
			<h1>PartA--start</h1>
				<app-content-part-b></app-content-part-b>
			<span>PartA--end</span>
		</app-content-part-a>
	</div>
</div>
登入後複製
/**** content.component.ts ****/
import { Component, OnInit, ViewChild } from &#39;@angular/core&#39;;

@Component({
	selector: &#39;app-content&#39;,
	templateUrl: &#39;./content.component.html&#39;,
	styleUrls: [&#39;./content.component.scss&#39;]
})

export class ContentComponent implements OnInit {
	// 2、获取视图 partA
	@ViewChild(&#39;partA&#39;) partA: any;
	constructor() { }
	ngOnInit() {}
	ngAfterViewInit(): void {
		// 3、调用 part-a 组件的 func() 方法
		this.partA.func();
	}
}
登入後複製
ngAfterContentInit( )對應的是

ngAfterViewInit()

(視圖節點初始化是在投影內容初始化之後)

ContentChild

和###ViewChild## #還存在複數的形式,即###ContentChildren###和###ViewChildren###,它們取到的是節點的一個集合,其他的沒有什麼區別######寫法如下:## #
import { Component, OnInit, ContentChild,ContentChildren ,QueryList } from &#39;@angular/core&#39;;
import { PartBComponent } from &#39;../part-b/part-b.component&#39;;

@Component({
	selector: &#39;app-content-part-a&#39;,
	templateUrl: &#39;./part-a.component.html&#39;,
	styleUrls: [&#39;./part-a.component.scss&#39;]
})
export class PartAComponent implements OnInit {

	@ContentChildren(PartBComponent) PartBs: QueryList<PartBComponent>;

	constructor() { }
	ngOnInit() {}
}
登入後複製
###更多程式相關知識,請造訪:###程式設計入門###! ! ###

以上是一文了解angular中的3種內容投影(單插槽、多插槽、有條件)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!