Ich habe den folgenden Code funktioniert, aber ich kann benutzerdefinierte Fehler von Eingabe nicht anzeigen oder deaktivieren Sie das Formular, wenn es nicht korrekt ist. Also ich möchte es in FormBuilder
konvertieren. Aber die Probleme sind, dass ich nicht weiß, wie man das Hinzufügen und Entfernen von Fragen im Formular verwaltet.Wie man ein evolutives Formular in angularjs2 mit FormBuilder (und dem ionischen v2-Framework) erstellt
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>
Es ist möglich :) Was scheint unmöglich? – jhhoff02
Weil ich so etwas tun kann, aber es ist nicht evolutiven \t this.addUserForm = this.formBuilder.group ({ \t \t \t 'username': [ '', Validators.required], \t \t \t 'Passwort': [ '', Validators.required], \t \t \t 'Rolle': [ 'Student'], \t \t \t 'Klasse': [ ''], \t \t \t 'givenCourse': [ ''] }); –