我有一个简单的组件如下:
import {Page} from 'ionic-framework/ionic';
import {Component} from 'angular2/core';
import {Data} from './data';
@Page({
selector: "main-page",
templateUrl: 'build/pages/main/main.html'
})
export class MainPage
{
submitted: number;
model: Array<number>;
constructor(){
this.submitted = false;
}
model = new Data('50', ['10', '20']);
onSubmit()
{
this.model.households.push('3');
}
backToForm()
{
this.submitted = false;
}
}
使用以下模板:MainPage
<ion-content padding class="getting-started" >
<h1>Random selection of houses generator</h1>
<div [hidden]='submitted'>
<form (ngSubmit)="onSubmit()" #dataForm="ngForm">
<ion-list>
<ion-item>
<ion-label floating>Number of houses in the village</ion-label>
<ion-input
type="number"
required
[(ngModel)]="model.totalhousehold"
ngControl = "totalhousehold"
></ion-input>
</ion-item>
</ion-list>
<button
secondary
[disabled]="!dataForm.form.valid"
>Generate random selection!</button>
</form>
</div>
<div [hidden]='!submitted'>
villages: {{ model.totalhousehold }}
Array: {{ model.households }}
<button (click)=backToForm()>
back to form
</button>
</div>
</ion-content>
这是我的data.ts:
export class Data {
constructor(
public totalhousehold: number,
public households: Array<number>,
) { }
}
在每次提交时,我都会在我的“家庭”数组中推送一个值并隐藏表单。返回表格显示表格。如果我修改表单值(totalhouseholds),我的数组(households)会正确显示。但是我再次提交而不修改值;显示的数组未更新。欢迎任何想法建议