2016-02-24 13 views
8

私は、親の子である異なるクラス(モデル)を持っています。すべての子は独自のフォームを持っていますが、親参照を持つComponentは子に応じて特定のフォームをレンダリングします。例:角2のコンポーネント間の多型

モデル

export interface Item { 
    title: string; 
} 

export class Exercise implements Item { 
    constructor(public title:string, public description:string); 
} 

export class Break implements Item { 
    constructor(public title:string, public time:number); 
} 

フォーム

@Component({ 
    selector: 'item-form', 
    template: `<item></item> 
    `, 
    inputs: ['model:item'] 
}) 
export abstract class ItemFormComponent { 
    model: Item; 
} 

@Component({ 
    selector: 'item-form', 
    template: ` 
     <form #exerciseForm="ngForm"> 
      <input type="text" class="form-control" required 
      [(ngModel)]="model.title" 
      ngControl="title" #name="ngForm" > 
      <input type="text" class="form-control" required 
      [(ngModel)]="model.description" 
      ngControl="desription" #name="ngForm" > 
     </form> 
    `, 
    providers: [ExerciseService], 
    inputs: ['model:exercise'] 
}) 
export class ExerciseFormComponent extends ItemFormComponent { 
    model = new Exercise("Title", "Description"); 

    constructor(private _exerciseService: ExerciseService) { 
     super(); 
    } 
} 

@Component({                       
    selector: 'item-form',                    
    template: ` 
     <form #exerciseForm="ngForm"> 
      <input type="text" class="form-control" required 
      [(ngModel)]="model.title" 
      ngControl="title" #name="ngForm" > 
      <input type="text" class="form-control" required 
      [(ngModel)]="model.time" 
      ngControl="number" #name="ngForm" > 
     </form> 
    `,              
    inputs: ['model:break']                   
})                                                   
export class BreakFormComponent extends ItemFormComponent {           
    model = new Break("Title", 10);                    

} 

のApp

@Component({ 
     selector: 'app', 
     template: ` 
      <h1>App</h1> 
      <div *ngFor="#item of items"> 
       <item-form [model]="item"></item-form> <!-- HERE IS WHERE FORM SHOULD BE INSERTED! --> 
      </div> 
     `, 
     directives: [ItemFormComponent] 
}) 
export class App { 
    items: Item[] = [new Exercise("Exercise", "Description"), new Break("Break", 10)]; 
} 
+1

TypeScriptは多態性をサポートしていますが、Angularアノテーションはサポートしていません。抽象スーパークラスではなく、ディレクティブフィールドにクラスの正確な名前を指定する必要があると思います。 – MatthewScarpino

+0

これは「誰が親の子ですか」ですか?本当に親子関係か、これはスーパークラスとサブクラスの関係ですか? –

+0

Angular cookbookの動的フォームレシピは、使用するべきコントロールを示すために派生クラスが使用する 'controlType'プロパティと、スイッチ変数として' controlType'を使用するswitch文を持つ単一質問コンポーネント質問が使用する可能性のあるそれぞれの差異コントロールのケース: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html これは可能な限り可能です。 – Ergwun

答えて

1

私はあなたのようなものが必要だと思います。

 <div *ngFor="#item of items"> 
      <item-form-a *ngIf="item instanceOf A" [model]="item"></item-form-a> 
      <item-form-b *ngIf="item instanceOf B" [model]="item"></item-form-a> 
     </div> 

私は活字体を使用していないとinstanceOfは角式でサポートされているかどうかわかりません。そうでない場合は、小切手をAppの機能に移動し、それを*ngIf="isInstanceOf(item, A)のように呼び出す必要があります。

+2

'* ngIf =" item instanceof A "'でテストしましたが、サポートされていません。私は* ngIf = "isInstanceOf(item、A)'を試しましたが、Aは常に定義されていません。 ? –

+0

それは何とか期待された。型はコンポーネントクラスのメンバーではなく、バインディングではコンポーネントの外にあるものを参照することはできません。したがって、Aは式のバインディングには使用できません。私はなぜ 'isA(item){item itemOf Aを返すかわからない。 } 'は動作しません。私はTSを使用していない私はちょうどタイプのチェックを見た。クラスの名前を含む文字列フィールドのような型をチェックできるプロパティをアイテムが提供するための回避策が必要なのかもしれません。 –

+0

@フランク:これは実際にはあなたの質問に答えていないので、そのようにマークするべきではありません。 –

関連する問題