好你可以試試這個。
基於鏈接。你可以試試這個代碼
在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