好你可以试试这个。
基于链接。你可以试试这个代码
在HTML
<ion-item>
<ion-label>place</ion-label>
<ion-select [(ngModel)]="selected">
<ion-option value="item" *ngFor="let item of options">
{{item.name}} {{item.price}}</ion-option>
</ion-select>
</ion-item>
<div padding>
<button ion-button (tap)="addOption()" block>Add Option</button>
<button ion-button (tap)="PostSelected()" block> Sync </button>
</div>
然后在你的页面组件。
import { Http, RequestOptions, Headers } from '@angular/http';
import 'rxjs/Rx';
//first declare your ngModel variable and you post array variable
selected:any;
SelectedArray = [];
addOption(){
this.SelectedArray.push(this.selected);
}
PostSelected(){
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({
headers: headers,
method: "POST"
});
let bodu = JSON.stringify(this.SelectedArray)
let result = this._http.post(link, body, options).timeout(30000);
result.subscribe(data => {
console.log('result sucess',data)
},error=>{
console.log('result error',data)
})
}
UPDATE
addOption(){
let validate = this.SelectedArray.filter(res=>{
return res == this.selected;
});
// if validate length is 0 the new value of this.selected is currently not found on this.SelectedArray so its ok to be push
if(validate.length == 0){
this.SelectedArray.push(his.selected)
}else{
// else do other thing
}
}
当我添加ngModel到离子选择我失去了我的数据由于某种原因。 – ob1
最初,选择框起作用,我可以选择一个项目,但它不再显示在页面上。然后第二次点击选择框没有数据。 :另外,发现其他人也有同样的问题,但他们的解决方案无法正常工作。 [链接](https://github.com/ionic-team/ionic/issues/8942) – ob1
这里是我的HTML页面的样子: '<离子项项-多输入= “真”> {{environment.name}} 环境 > } ' –
ob1