2016-06-30 7 views
0

次のコードを使用していますが、入力からカスタムエラーを表示したり、フォームが正しくない場合は無効にすることはできません。ですので、FormBuilderに変換したいと思います。しかし問題は、フォームの追加と削除の管理方法がわからないことです。FormBuilder(およびiony v2フレームワーク)を使用してangularjs2でEvolutiveフォームを作成する方法

import {Component} from '@angular/core'; 
 
import {NavController} from 'ionic-angular'; 
 

 
    
 
@Component({ 
 
\t templateUrl: 'build/pages/add-question/add-question.html', 
 
\t providers: [BddService] 
 
}) 
 
export class AddQuestionPage { 
 

 
\t public questions; 
 

 
\t constructor(private nav: NavController, private bddService : BddService) { 
 
\t \t this.questions = []; 
 
\t \t this.addQuestion(); 
 
\t } 
 

 
\t public addQuestion(){ 
 
\t \t var question ={ 
 
\t \t \t category:'', 
 
\t \t \t material:'', 
 
\t \t \t chapter:'' 
 
\t \t } 
 
\t \t this.questions.push(question); 
 
\t } 
 

 
\t public removeQuestion(index: number){ 
 
\t \t if(this.questions.length > 1){ 
 
\t \t \t delete this.questions[index]; 
 
\t \t } 
 
\t } 
 

 
}
<ion-content class="add-question"> 
 
    <form #f="ngForm" (ngSubmit)="saveQuestion()"> 
 
    <ion-list> 
 
     <div class="question" *ngFor="let question of questions; let i = index "> 
 
     <ion-item> 
 
      <ion-label stacked>Category</ion-label> 
 
      <ion-input type="text" [(ngModel)]="questions[i].category" required> 
 
      </ion-input> 
 
     </ion-item> 
 
     <ion-item> 
 
      <ion-label stacked>Material</ion-label> 
 
      <ion-input type="text" [(ngModel)]="questions[i].material" required> 
 
      </ion-input> 
 
     </ion-item> 
 
     </div> 
 
     <ion-item> 
 
     <button type="button" (click)="addQuestion()" full light>Add Question</button> 
 
     </ion-item> 
 
     <ion-item> 
 
     <button type="submit" full light>Save Question</button> 
 
     </ion-item> 
 
    </ion-list> 
 
    </form> 
 
</ion-content>

+0

それは不可能だと思われるものはありますか?私はこのような何かを行うことができますが、それは進化ではないです – jhhoff02

+0

ので\t this.addUserForm = this.formBuilder.group({ \t \t \t 'ユーザ名':[ ''、Validators.required]、 \t \t \t 'パスワード': [ ''、Validators.required]、 \t \t \tの役割 ':[ '学生']クラス、\t \t \t '':[ '']、 \t \t \t 'givenCourse':[ '']} ); –

答えて

1

この試してみてください:あなたはカスタム検証を追加したい場合は、あなたがaddQuestion()にそれらを追加することができ

<ion-content padding class="evolutive"> 
    <form [ngFormModel]="evolutiveForm" (submit)="saveEvolutive($event, questions)"> 
     <ion-list> 
      <ion-item-group *ngFor="let q of questions; let i = index" ngControlGroup="{{i}}"> 
       <ion-item-divider light>Question {{+i+1}} <ion-icon name="close" (click)="removeQuestion(i)" item-right></ion-icon></ion-item-divider> 
       <ion-item> 
        <ion-label>Category</ion-label> 
        <ion-input ngControl="category" [(ngModel)]="q.category"></ion-input> 
       </ion-item> 
       <ion-item> 
        <ion-label>Material</ion-label> 
        <ion-input ngControl="material" [(ngModel)]="q.material"></ion-input> 
       </ion-item> 
      </ion-item-group> 
     </ion-list> 
     <button block type="button" (click)="addQuestion()">Add Question</button> 
     <button block type="submit" [disabled]="!evolutiveForm.valid">Save Player</button> 
    </form> 
</ion-content> 

import {Component} from "@angular/core"; 
import {NavController} from "ionic-angular"; 
import {FormBuilder, ControlArray, Validators} from "@angular/common"; 

class Question { 
    category: string; 
    material: string; 
    chapter: string; 
} 

@Component({ 
    templateUrl: "build/pages/evolutive/evolutive.html" 
}) 
export class EvolutivePage { 

    questions: Array<Question>; 
    evolutiveForm: ControlArray; 

    constructor(public nav: NavController, private fb: FormBuilder) { 

     this.questions = []; 
     this.evolutiveForm = new ControlArray([]); 
    } 

    saveEvolutive($event, questions) { 
     $event.preventDefault(); 
     console.log(questions); 
    } 

    addQuestion(): void { 
     let q = new Question(); 
     this.evolutiveForm.push(this.fb.group({ 
      category: [q.category, Validators.required], 
      material: [q.material, Validators.required] 
     })); 
     this.questions.push(q); 
    } 

    removeQuestion(i: number): void { 
     this.questions.splice(i, 1); 
     this.evolutiveForm.removeAt(i); 
    } 
} 

そして、あなたのテンプレートのために新しいを作成する関数質問ごとにそれだけで、すべてのフィールドが埋められていることを確認します。

+0

特定の質問を削除できるようにコードを更新しました。 – morphatic

+0

ありがとう、それは動作します。あなたのための最後の質問は本当に良いと思うので、ああ。私はオプションの配列をしたい場合、私は同じように行うことができますか? –

+0

ええ、ちょうど各質問と一緒にフィールドを追加してください – morphatic

関連する問題