2017-08-08 124 views
2

我一直在尋找相當一段時間,一直沒能找到任何具體的如何做到這一點。最接近的東西,我可以找到這個:How to get the value from ion-select option離子2角2從離子選擇的形式發送http post

我想弄清楚如何收集選定或輸入到一個頁面上的數據,並將其放入一個數組爲http post? ..將不勝感激任何例子或指導我一些很好的文檔。

離子信息:

科爾多瓦CLI:7.0.1 離子Framework版本:3.5.3 離子CLI版本:2.1.12 離子應用程序庫版本:2.1.7 離子應用腳本版本:2.0。 2個 IOS部署的版本:1.9.1 IOS-SIM版本:6.0.0 OS:OS X埃爾卡皮坦 節點版本:V4.3.2 的Xcode版本:8.2.1的Xcode版本構建8C1002

回答

1

好你可以試試這個。

基於鏈接。你可以試試這個代碼

在HTML

<ion-item> 
    <ion-label>place</ion-label> 
    <ion-select [(ngModel)]="selected"> 
    <ion-option value="item" *ngFor="let item of options"> 
     {{item.name}} &nbsp;&nbsp;{{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 
     } 
    } 
+0

當我添加ngModel到離子選擇我失去了我的數據由於某種原因。 – ob1

+0

最初,選擇框起作用,我可以選擇一個項目,但它不再顯示在頁面上。然後第二次點擊選擇框沒有數據。 :另外,發現其他人也有同樣的問題,但他們的解決方案無法正常工作。 [鏈接](https://github.com/ionic-team/ionic/issues/8942) – ob1

+0

這裏是我的HTML頁面的樣子: '<離子項項-多輸入= 「真」> {{environment.name}} 環境 > } ' – ob1