重置离子选项值(ionChange) - 离子2

时间:2017-03-02 06:54:59

标签: asp.net html5 typescript ionic2

有谁知道如何重置“ion-select”的“ion-option”值。我有两个离子选择控件,我希望我的第一个离子选择(selectedPLocation)更改ionChange上我的第二个离子选择(selectedLocation)的值。我能够通过设置null删除所选,但我无法更改selectedLocation的值。有谁知道如何重置我的离子选项的值?

我目前正在使用VS2015作为我的IDE。

HTML:

<ion-list> 
     <ion-item>
            <ion-label>Parent Location</ion-label>
            <ion-select [(ngModel)]="selectedPLocation" (ionChange)="loadLocation()">
                <ion-option *ngFor="let parentLocation of parentLocations; let i=index" [value]="parentLocation.Key">{{parentLocation.Id}}</ion-option>
            </ion-select>
     </ion-item>
     <ion-item>
            <ion-label>Location</ion-label>
            <ion-select [(ngModel)]="selectedLocation">
                <ion-option id="locationID" *ngFor="let location of locations; let i=index" [value]="location.Key">{{location.Id}}</ion-option>
            </ion-select>
     </ion-item>
</ion-list>

打字稿:

public loadLocation() {
let loader = this.loadingCtrl.create({
    content: "Please wait..."
});
loader.present();

this.selectedLocation = null; //Reset selected value only
this.locations = null; //Tried this but can't seem to reset the values

this.locationService.GetLocations(this.global.getApiUrl(), this.selectedSite, this.selectedPLocation).then(data => {
    loader.dismiss();
    this.locations = data;
}).catch((err) => {
    loader.dismiss();
    let alert = this.alertCtrl.create({
        title: 'Error',
        subTitle: err,
        buttons: ['OK']
    });
    alert.present();
});}

1 个答案:

答案 0 :(得分:0)

我解决了自己的错误,这是由于我的Typescript代码调用了我的Web服务API(GetLocation)。

以前的TypeScript代码:

public GetLocations(apiUrl, siteKey, pLocationKey) {

    return new Promise((resolve, reject) => {
        this.http.get(apiUrl + "site/" + siteKey + "/location/" + pLocationKey)
            .map(res => res.json())
            .subscribe(data => {
                this.Locations = data;     //Error Here
                resolve(this.Locations);;  //Error Here
            },
            err => {
                reject(err);
            });
    });
}

更正TypeScript代码:

public GetLocations(apiUrl, siteKey, pLocationKey){

    return new Promise((resolve, reject) => {
        this.http.get(apiUrl + "site/" + siteKey + "/location/" + pLocationKey)
          .map(res => res.json())
          .subscribe(data => {
              resolve(data);     //Corrected
          },
          err =>{
            reject(err);
          });
    });
}