2017-07-02 5 views
1

私はこの2日間でこれを探していましたので、私は助けを求めることにしました。 ParentComponentという名前の親コンポーネントがあり、SomeComponentという子コンポーネントもあるとします。で定義されたNativeScript - 親から子コンポーネントにコンテンツを渡す

import {Component} from "@angular/core"; 
import {SomeComponent} from "../some/where/..."; 

@Component({ 
    selector: "parent", 
    template: ` 
     <SomeComponent> 
      <Label text="Something here"></Label> 
      <Label text="Something else here"></Label> 
     </SomeComponent> 
    `, 

}) 
export class ParentComponent {} 

上記の例を考えると、どのように私は "> < SomeComponent" 内のコンテンツを取得することができます:

SomeComponentテンプレートは次のようになります。

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

@Component({ 
    selector: "SomeComponent", 
    template: ` 
     <ActionBar title="TestApp"> 
     </ActionBar> 
     <StackLayout style="margin-top:20;"> 
      <Label text="Somenthing on top"></Label> 

      #CONTAINER CONTENT HERE# 

      <Label text="Something in the bottom"></Label> 
     </StackLayout> 
    `, 

}) 
export class SomeComponent {} 

..and parentComponentにテンプレートは次のようになり私のParentComponentは、予約済みの#CONTAINER CONTENT HERE#エリアのSomeComponentに正しく表示されますか?

理論的には、それは私がこのようなものに終わるかのようである:

<ActionBar title="TestApp"> 
</ActionBar> 
<StackLayout style="margin-top:20;"> 
    <Label text="Somenthing on top"></Label> 

    <Label text="Something here"></Label> 
    <Label text="Something else here"></Label> 

    <Label text="Something in the bottom"></Label> 
</StackLayout> 

何かのように私が行うために使用私が仕事を得ることができないことを、ネイティブに反応することが非常に簡単になりますNS。

ありがとうございます。

答えて

1

ng-contentタグを使用して、コンテンツを親コンテナから子にトランスコードすることができます。また、あなたがの内部の作業例を見ることができます

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

@Component({ 
    selector: "SomeComponent", 
    template: ` 
     <ActionBar title="TestApp"> 
     </ActionBar> 
     <StackLayout style="margin-top:20;"> 
      <Label text="Somenthing on top"></Label> 

      <ng-content></ng-content> 

      <Label text="Something in the bottom"></Label> 
     </StackLayout> 
    `, 

}) 
export class SomeComponent {} 

あなたはトランスクルーの詳細を読むことができ、ここでhttps://toddmotto.com/transclusion-in-angular-2-with-ng-content

:私は、次のようになりますどのあなたがSomeContentコンポーネントにNG-コンテンツを追加するために必要なすべてを、信じてスライドプラグイン私が書いたhttps://github.com/TheOriginalJosh/nativescript-ngx-slides/blob/master/slides/app/slides/slides.component.ts#L40

+0

うん。私は正確にそれを試みたが、それは動作しませんでした。それは私がやっている何か他のものかもしれません。少なくとも今、私はこれがこれを行う方法であることを知っています。 TNX – alvesdm

+1

私は "宣言" に私のコンポーネントを追加していなかった。 @NgModule({ 宣言:[ AppComponentは、 SomeComponent ]、 TNX @theOriginalJosh – alvesdm

+0

Npが、私自身は頻繁にそれを忘れてしまいました。 – theOriginalJosh

関連する問題