新聞中心
這篇文章主要介紹“angular中怎么進行內(nèi)容投影”,在日常操作中,相信很多人在angular中怎么進行內(nèi)容投影問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”angular中怎么進行內(nèi)容投影”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了銅官免費建站歡迎大家使用!
一、 ng-content
進行內(nèi)容投影
1.1 ng-content
ng-content
元素是一個用來插入外部或者動態(tài)內(nèi)容的占位符
。父組件將外部內(nèi)容
傳遞給子組件,當 Angular
解析模板時,就會在子組件模板中 ng-content
出現(xiàn)的地方插入外部內(nèi)容。
我們可以使用內(nèi)容投影來創(chuàng)建可重用的組件。這些組件有相似的邏輯和布局,并且可以在許多地方使用。一般我們在封裝
一些公共組件的時候經(jīng)常會用到。
1.2 為什么使用內(nèi)容投影
定義一個 button 組件:
button-component.ts
@Component({ selector: '[appButton]', template: ` ` }) export class AppButtonComponent {}
這個 button 組件的目的是在按鈕內(nèi)部加一個搜索的圖標,我們實際使用如下:
我們發(fā)現(xiàn)組件只會展示搜索圖標, 按鈕的文本不會展示,能你會想到最常使用的 @Input
裝飾器,但是如果我們不只是想傳文本進來,而是傳一段 html
進來呢?此時就會用到 ng-content
。
1.3 單插槽內(nèi)容投影
內(nèi)容投影的最基本形式是單插槽內(nèi)容投影
。
單插槽內(nèi)容投影是指創(chuàng)建一個組件,我們可以在其中投影一個組件。
以 button 組件為例,創(chuàng)建一個單槽內(nèi)容投影:
button-component.ts
@Component({ selector: '[appButton]', template: `` }) export class AppButtonComponent {}
實際使用如下:
可以發(fā)現(xiàn),現(xiàn)在這個 button 組件的效果是即顯示了搜索圖標,又顯示了按鈕的文本(click)。即把 中間的內(nèi)容
投影
到了組件的
位置。
ng-content 元素是一個占位符,它不會創(chuàng)建真正的 DOM 元素。ng-content 的那些自定義屬性將被忽略。
1.4 多插槽內(nèi)容投影
一個組件可以具有多個插槽
,每個插槽可以指定一個 CSS 選擇器
,該選擇器會決定將哪些內(nèi)容放入該插槽。該模式稱為多插槽內(nèi)容投影
。使用此模式,我們必須指定希望投影內(nèi)容出現(xiàn)在的位置
。可以通過使用 ng-content
的 select
屬性來完成此任務。
組件模板含有
多個
ng-content
標簽。為了區(qū)分投影的內(nèi)容可以投影到對應
ng-content 標簽
,需要使用ng-content
標簽上的select
屬性作為識別。select
屬性支持標簽名
、屬性
、CSS 類
和:not 偽類
的任意組合。不添加
select
屬性的ng-content
標簽將作為默認插槽
。所有未匹配的投影內(nèi)容都會投影在該ng-content
的位置。
以 button 組件為例,創(chuàng)建一個多槽內(nèi)容投影:
button-component.ts
@Component({ selector: '[appButton]', template: `` }) export class AppButtonComponent {}
實際使用如下:
1.5 ngProjectAs
在某些情況下,我們需要使用 ng-container
把一些內(nèi)容包裹起來傳遞到組件中。大多數(shù)情況是因為我們需要使用結(jié)構(gòu)型指令像 ngIf
或者 ngSwitch
等。。
在下面的例子中,我們將 header
包裹在了 ng-container
中。
@Component({ selector: 'app-card', template: `` }) export class AppCardComponent {}
使用:
Angular
One framework. Mobile & desktop.
由于 ng-container
的存在,header
部分并沒有被渲染到我們想要渲染的插槽中,而是渲染到了沒有提供任何 selector
的 ng-content
中。
在這種情況下,我們可以使用 ngProjectAs
屬性。
在上面的 ng-container
加上這個屬性,就可以按照我們的期望來渲染了。
Angular
One framework. Mobile & desktop.
二、 有條件的內(nèi)容投影
如果你的元件需要有條件地渲染內(nèi)容或多次渲染內(nèi)容,則應配置該元件以接受一個 ng-template
元素,其中包含要有條件渲染的內(nèi)容。
在這種情況下,不建議使用 ng-content
元素,因為只要元件的使用者提供了內(nèi)容,即使該元件從未定義 ng-content
元素或該 ng-content
元素位于 ngIf
語句的內(nèi)部,該內(nèi)容也總會被初始化。
使用 ng-template
元素,你可以讓元件根據(jù)你想要的任何條件顯式渲染內(nèi)容,并可以進行多次渲染。在顯式渲染 ng-template
元素之前,Angular
不會初始化
該元素的內(nèi)容。
2.1 ng-container
既不是一個組件,也不是一個指令,僅僅是一個特殊的tag而已。 使用 ng-container
渲染所包含的模板內(nèi)容,不包含自身。
angular代碼片段
My name is wyl.
What is you name?
瀏覽器調(diào)試窗口,可以發(fā)現(xiàn)
標簽消失了,并沒有起任何作用
My name is wyl.
What is you name?
使用場景,如下,在我們需要
遍歷
或if 判斷
時,它可以承擔一個載體
的作用
- {{ item .name}}
- {{ item .age}}
- {{ item .sex}}
另外,ng
中常見錯誤之一的 for
和 if
不能寫在同一標簽上(在一個宿主元素上只能應用一個結(jié)構(gòu)型指令),利用 ng-container
標簽可以在實現(xiàn)功能的基礎上減少層級的嵌套。
2.2 ng-template
先來看下面一段代碼
In template, no attributes.
In ng-container, no attributes.
瀏覽器輸出結(jié)果是:
In ng-container, no attributes.
即
中的內(nèi)容不會顯示。當在上面的模板中添加 ngIf
指令:
ngIf with a ng-template.
ngIf with an ng-container.
瀏覽器輸出結(jié)果是:
ngIf with a ng-template. ngIf with an ng-container.
2.3 ng-template
和
的配合使用
暫時搜索不到您要的數(shù)據(jù)喔 快快開始獲取吧!
2.4 ngTemplateOutlet
插入 ng-template
創(chuàng)建的內(nèi)嵌視圖。 ngTemplateOutlet
是一個結(jié)構(gòu)型指令
,接收一個 TemplateRef
類型的值;
I am span in template {{title}}
*ngTemplateOutlet = "templateRefExp; content: contentExp "
templateRefExp:
ng-template
元素的#ID
contextExp:
可空參數(shù)
content
是一個對象,這個對象可以包含一個$implicit
的key
作為默認值, 使用時在模板
中用let-key
語句進行綁定content
的非默認字段需要使用let-templateKey=contentKey
語句進行綁定
使用如下:
@Component({ selector: 'ng-template-outlet-example', template: `
Hello Hello {{name}}! Ahoj {{person}}! ` }) class NgTemplateOutletExample { myContext = {$implicit: 'World', localSk: 'Svet'}; }
2.5 利用 ngTemplateOutlet
進行內(nèi)容投影
@Component({ selector: 'app-card', template: `` }) export class AppCardComponent { @ContentChild('header', { static: true }) headerTemplate: TemplateRef; public title = 'Test'; public otherDate = { auth: 'me', name: 'appCard' }; }
使用
Angular
{{label}} (Test) and {{otherDate | json}} ({auth: 'me', name: 'appCard'})
到此,關于“angular中怎么進行內(nèi)容投影”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前名稱:angular中怎么進行內(nèi)容投影
網(wǎng)頁URL:http://ef60e0e.cn/article/gphjcd.html