[英]Custom Styling on <ng-content> in angular2 not working ?
我正在嘗試使用內聯css設置<ng-content>
樣式,但似乎樣式不適用於ng-content,我必須為樣式做些其他事情?
<ng-content class="red"></ng-content> <p class="red">hello</p>
這里的班級紅色在p
上工作但不在
::content
被忽略。
這個結束了
您可以使用::content
選擇器
styles: ['.red {color:red} :host >>> upper {color:green}']
要么
styles: ['.red {color:red} :host >>> * {color:green}']
如果有LESS用戶,似乎LESS編譯器不喜歡
>>>
語法,所以你需要為它添加一個別名,例如。@deep: ~">>>";
然后使用@{deep} { /* your deep styles here */ }
另見本討論https://github.com/angular/angular/issues/7400#issuecomment-246922468
您可以使用
::content
選擇器
styles: ['.red {color:red} ::content >>> upper {color:green}']
要么
styles: ['.red {color:red} ::content >>> * {color:green}']
根據web組件spec ::content
應該足夠了, >>>
不應該被要求但是沒有它就沒有應用樣式。
實際上,它是因為被輸入內容所取代。 該元素不存在於內存中的DOM中。
關於你的樣品,使用upper
:
<upper>Some text</upper>
具有以下模板:
<ng-content class="red"></ng-content> <p class="red">hello</p>
你將在DOM中擁有它:
<upper _nghost-dks-2="">
Some text <p _ngcontent-dks-2="" class="red">hello</p>
</upper>
因此,您不能在ng-content
上使用class="red"
。
嘗試將css添加到父css文件中: app/src/style.css
不要進入組件的styleUrls的css文件(你在組件中包含了這樣的styleUrls: ['design.css']
)
只需包裝ng-content並使用css在里面選擇它?
<div class="red"><ng-content></ng-content></div>
CSS:
.red > * {
color: red;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.