角2は、コンポーネントの子孫要素を照会するための,@ViewChildren
,@ContentChild
および@ContentChildren
のデコレータを提供します。最初の2つと後の2つの違いは何ですか?@ViewChildと@ContentChildの違いは何ですか?
答えて
私は(それがWebコンポーネントから来ている、hereを参照してください)シャドウDOMとライトDOM用語を使用して、あなたの質問にお答えします。一般的には:
- シャドウDOMは - (コンポーネントのクリエイターとして)あなたでを定義し、エンドユーザーから隠されているコンポーネントの内部DOMです。例えば:
@Component({ selector: 'some-component', template: ` <h1>I am Shadow DOM!</h1> <h2>Nice to meet you :)</h2> <ng-content></ng-content> `; }) class SomeComponent { /* ... */ }
- ライトDOM - コンポーネントにコンポーネント供給エンドユーザーことがDOMです。たとえば:
@Component({
selector: 'another-component',
directives: [SomeComponent],
template: `
<some-component>
<h1>Hi! I am Light DOM!</h1>
<h2>So happy to see you!</h2>
</some-component>
`
})
class AnotherComponent { /* ... */ }
だから、あなたの質問への答えは非常に簡単です:
@ViewChildren
と@ContentChildren
の違いは、シャドウDOM内の要素のための@ViewChildren
見ながら、彼らのために@ContentChildren
表情でライトDOM。
Minko Gechewのブログエントリhttp://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/が私には分かりやすい。 @ContentChildrenはコンテンツ投影によって挿入された子(
私には' (@ContentChildren'の代わりに@TemplateChildren'や '@ContentChildren'の代わりに)' @HostChildren'はもっと良い名前になっていました。私たちが話していることのすべては、ビュー関連であり、wrtバインディングです – superjos
'@ ViewChildren' ==あなた自身の子;' @ ContentChildren' ==他の人の子ども – candidJ
名の通り@ViewChild
と@ViewChildren
のみ直接あなたのビューテンプレート上にある要素を見てのに対し、@ContentChild
と@ContentChildren
クエリは、ビューの<ng-content></ng-content>
要素内の既存のディレクティブを返します。
あなたが@ContentChild(ren)に戻ってくるコンポーネントをビューに持たない限り@ViewChild(ren)を使用しますか? –
アンギュラ接続からこのビデオでは、comp-a
がContentChild
とcomp-b
ViewChild
であり、
@Component({
template: `
<my-widget>
<comp-a/>
</my-widget>
`
})
class App {}
@Component({
selector: 'my-widget',
template: `<comp-b/>`
})
class MyWidget {}
my-widget
の観点から優れた情報を持っています。 CompomentChildren
およびViewChildren
は、xChildが1つのインスタンスを返す間に繰り返し可能な値を返します。
- 1. 角度2 - ViewChildもContentChild
- 2. Angular2のViewChildとは何ですか?
- 3. ViewChild/ContentChildを使用してディレクティブからディレクティブを見つける方法は?
- 4. $と$$の違いは何ですか?
- 5. `##`と `hashCode`の違いは何ですか?
- 6. typedefとの違いは何ですか?
- 7. NodeJS:「./ ..」と「..」の違いは何ですか?
- 8. {}())と{})の違いは何ですか?
- 9. `&`と `ref`の違いは何ですか?
- 10. `==`と `is`の違いは何ですか?
- 11. difftimeと ' - 'の違いは何ですか?
- 12. #{} $ {}と%{}の違いは何ですか?
- 13. ⊕と+の違いは何ですか?
- 14. アペンダーと〜の違いは何ですか?
- 15. "./"と "../"の違いは何ですか?
- 16. "$^N"と "$ +"の違いは何ですか?
- 17. %Qと%{}の違いは何ですか?
- 18. バックスティックと$()の違いは何ですか?
- 19. A`と ``の違いは何ですか[:] `
- 20. パッケージのインポートobjと何かの違いをインポートパッケージとPythonの何かの違いは何ですか?
- 21. METADATAとMETADATA.jl:違いは何ですか?
- 22. twistdとnohup&:違いは何ですか?
- 23. libspeとlibspe2?違いは何ですか?
- 24. Backbone.jsとPusherapp - 違いは何ですか?
- 25. wp_link_pagesとthe_posts_pagination→違いは何ですか?
- 26. $ dirtyと$ invalid:違いは何ですか?
- 27. XPathの//と?/の違いは何ですか?の使用は何
- 28. フレームとオブジェクトの違いは何ですか?フレームとオブジェクトの違いは何ですか?
- 29. リストと何か([_])と何か(_)の違い
- 30. 違いは何ですか?
このリンクは、以下の回答を読んだ後、http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/に役立ちました。乾杯:) –