3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ng-contentで複数の要素を出し分けるselectディレクティブ

Last updated at Posted at 2019-08-07

##Angularのng-content
AngularでコンポートネントがHTML要素を引数として受け取りたい時、ng-content要素をりようします。

<div>
  <ng-content></ng-content>
</div>

##複数の要素を受け取りたい場合
複数の要素を受け取りたい場合、select=h2のように、slectディレクティブにcssセレクターを付与することでng-contentの要素を出し分けることができます。

example.component.html
<div>
  <ng-content select="header"></ng-content>
</div>
<div>
  <ng-content select="footer"></ng-content>
</div>

利用例

<example-component>
  <header>タイトル</header>
  <footer>フッター</footer>
</example-component>

結果

<div>
   <header>タイトル</header>
</div>
<div>
  <footer>フッター</footer>
</div>  

##参考
https://medium.com/@IMM9O/angular-content-projection-the-complete-guide-45d3bc1692f2

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?