我有一个在FormGroup中带有离子输入的页面。当他们转到页面时,数据将从服务器加载。当数据进来时,我希望用来自服务器的数据填充离子输入,然后用户可以编辑和保存这些数据。我无法获得离子输入来显示数据。(它保持空白(
这是页面:
import { Component,ViewChild } from '@angular/core';
import { Validators, FormBuilder, FormGroup } from "@angular/forms";
import { NavController, App, MenuController, NavParams, AlertController,Content } from 'ionic-angular';
import {DomSanitizer,SafeResourceUrl} from '@angular/platform-browser';
import { TabsPage } from '../tabs/tabs';
// Providers
import { ProjectDetailService } from '../../providers/project-detail-service';
@Component({
selector: 'page-form',
templateUrl: 'form.html',
})
export class FormPage {
@ViewChild(Content) content: Content;
newObject: FormGroup;
object: any = {};
objectKey: string = "";
pageTitle: string = "Create New";
videoURL: SafeResourceUrl;
sanitizer: DomSanitizer;
updating: boolean = false;
constructor( public formBuilder: FormBuilder,
public service: ProjectDetailService,
public navParams: NavParams,
public app: App,
public alertCtrl: AlertController,
sanitizer: DomSanitizer ) {
this.sanitizer = sanitizer;
this.newObject = this.formBuilder.group({
name: this.object.name
});
}
setData()
{
this.newObject.value.name = this.object.name;
//none of these work:
//this.content.resize();
//window.location.reload();
//this.app.getRootNav().setRoot(this.app.getRootNav().getActive().component);
}
ionViewDidLoad()
{
this.objectKey = this.navParams.get('projectKey');
console.log("objectkey="+this.objectKey)
this.service.getProject(this.objectKey).subscribe( ( data: any ) => {
this.object = data;
this.setData();
})
}
这是 html:
<ion-content padding>
<form [formGroup]="newObject" (ngSubmit)="save()">
<ion-item>
<ion-label>Project Name</ion-label>
<ion-input type="text" formControlName="name"></ion-input>
</ion-item>
</form>
</ion-content>
我认为 FormBuilder 不是一个双向绑定器 而是使用像这样的简单双向绑定
<ion-input type="text" [(ngModel)]="name" formControlName="name"></ion-input>
和访问为
this.name = 'something';