Ich versuche zu verwenden Formbuilder in einer Seite, die ich in Ionic haben zu verwenden 2.Wie Angular 2 der Formbuilder zwischen mehreren Komponenten
Als erstes ist hier meine Umgebung Details ist: Laufen auf 10 Windows und ionische läuft - -Ausführung gibt mir 2.0.0-beta.35
Hier ist ein Teil meiner package.json Datei:
...
"@angular/common": "2.0.0-rc.3",
"@angular/compiler": "2.0.0-rc.3",
"@angular/core": "2.0.0-rc.3",
"@angular/forms": "^0.3.0",
"@angular/http": "2.0.0-rc.3",
"@angular/platform-browser": "2.0.0-rc.3",
"@angular/platform-browser-dynamic": "2.0.0-rc.3",
"ionic-angular": "2.0.0-beta.10",
"ionic-native": "1.3.2",
"ionicons": "3.0.0"
...
Zweitens hier sind die beiden wichtigsten Dateien beteiligt:
insight.ts
import { Component } from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {
REACTIVE_FORM_DIRECTIVES,
FormBuilder,
FormControl,
FormGroup
} from '@angular/forms';
import { App, Insight } from '../../models';
import { InsightProvider } from '../../providers/insight/insight.service';
import { InsightImage, InsightLabel, InsightLink, InsightQuestion, InsightThought, InsightTodo, InsightVideo } from './shared';
@Component({
templateUrl: 'build/pages/insight/insight.html',
directives: [REACTIVE_FORM_DIRECTIVES, InsightImage, InsightLabel, InsightLink, InsightQuestion, InsightThought, InsightTodo, InsightVideo],
providers: [App, InsightProvider, FormBuilder]
})
export class InsightPage {
canAdd: boolean;
showDetails: boolean;
newInsight: Insight;
insightForm: FormGroup;
constructor(private insightProvider: InsightProvider,
private params: NavParams) {
this.insightForm = new FormGroup({
type: new FormControl('', []),
todo: new FormControl('', []),
checked: new FormControl(false, []),
imageUrl: new FormControl('', []),
link: new FormControl('', []),
url: new FormControl('', []),
label: new FormControl('', []),
question: new FormControl('', []),
answer: new FormControl('', []),
title: new FormControl('', []),
details: new FormControl('', []),
});
}
ngOnInit() {
this.canAdd = false;
this.showDetails = true;
}
addNewInsight() {
if (this.newInsight.type) {
this.insightProvider.createInsight(this.newInsight)
.subscribe(response => {
this.newInsight.setId(response.data.id);
this.newInsight.title = '';
console.log(response);
});
}
}
deleteClicked(index: number) {
console.log('Clicked on ' + index);
this.insightProvider.deleteInsight(this.newInsight)
.subscribe(data => {
console.log(data);
});
}
}
insight.html
<form [ngFormModel]="insightForm" (ngSubmit)="createNewInsight()">
<ion-item>
<ion-label for="type">Insight Type</ion-label>
<ion-select name="type" id="type" [formControl]="type">
<ion-option value="label">Label</ion-option>
<ion-option value="thought">Thought</ion-option>
<ion-option value="link">Link</ion-option>
<ion-option value="question">Question</ion-option>
<ion-option value="todo">Todo</ion-option>
<ion-option value="image">Image</ion-option>
<ion-option value="video">Video</ion-option>
</ion-select>
</ion-item>
<div [ngSwitch]="type">
<insight-image [form]="insightForm" *ngSwitchCase="'image'"></insight-image>
<insight-label [form]="insightForm" *ngSwitchCase="'label'"></insight-label>
<insight-link [form]="insightForm" *ngSwitchCase="'link'"></insight-link>
<insight-question [form]="insightForm" *ngSwitchCase="'question'"></insight-question>
<insight-thought [form]="insightForm" *ngSwitchCase="'thought'"></insight-thought>
<insight-todo [form]="insightForm" *ngSwitchCase="'todo'"></insight-todo>
<insight-video [form]="insightForm" *ngSwitchCase="'video'"></insight-video>
</div>
<button type="submit" block primary text-center (click)="addNewInsight()" [disabled]="!newInsight.type">
<ion-icon name="add"></ion-icon> Add Insight
</button>
</form>
Wie Sie sehen können, ich versuche das passieren FormGroup-Objekt in mehrere Komponenten, so dass ich sie verwenden könnte. Hier
ist ein Beispiel dafür, was eine der Komponenten aussieht (Minimalversion jetzt):
<ion-item>
<ion-label floating for="link">Link</ion-label>
<ion-input type="text" name="link" id="link" [formControl]="link"></ion-input>
</ion-item>
<ion-item>
<ion-label floating for="url">URL</ion-label>
<ion-input type="text" id="url" name="url" [formControl]="url"></ion-input>
</ion-item>
Das Problem, das ich bin vor gerade jetzt dieser Fehler ist:
Was ich glaube, ist, dass der FormBuilder nach den angegebenen Namen sucht, die ich in meiner Typoskriptdatei deklariere (wie todo, imageUrl, link, usw.), aber da es in meinen anderen Komponenten ist, ist es fehlerhaft, weil es nicht dort ist .
Was könnte der Grund für diesen Fehler sein? Ich habe online gesucht und konnte keine verwandten Probleme finden.
FYI, der Grund, warum ich sie in Komponenten und nicht auf der gleichen Seite brauche, ist, weil in Zukunft die Funktionalität für jede Eingabe anders sein wird, also benötigt jeder Komponente eine "Single Responsibility".
Vielen Dank im Voraus
Sie nicht Version von Ionic2 erwähnt haben Sie verwenden, kann es wichtig sein. Ich bin mir nicht sicher, aber der Fehler sagt "unspezifizierter Name", also versuchen Sie 'ngControl =" url "' anstelle von '[formControl] =" url "' – dafyk
Ich habe gerade meine Frage aktualisiert, um meine Umgebungsdetails an der Spitze zu enthalten. Danke, dass du mich daran erinnerst, dich über meine Umgebung zu informieren. Ich habe versucht ngControl = "url", aber ich bekomme jetzt "Kein Provider für ControlContainer". Ich habe "ControlContainer" als Provider in meiner Komponente hinzugefügt, erhalte aber denselben Fehler. Nur für den Fall, ich habe versucht, formControl = "url" Ich bekomme den gleichen Fehler –
Nun Ionic2 ist noch Beta, erwarten einige brechen Änderungen gemäß [Formulare bevorstehenden Änderungsvorschlag] (https://docs.google.com/document/u/1/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub). Meine nächste Schätzung ist die Verwendung von 'formControlName =" url "' – dafyk