2017-05-20 4 views
0

my ionic2アプリはコードバプラグインからデータを取得しますが、入力値は自動更新されませんでした。 私のデモコード:更新値auto、ionic2更新値はページリフレッシュなし

import { Component } from '@angular/core'; 
import { NavController, Events } from 'ionic-angular'; 

declare var AMapPlugin; 

@Component({ 
    selector: 'page-hello-ionic', 
    templateUrl: 'hello-ionic.html' 
}) 

export class HelloIonicPage { 

    gps: string = ''; 
    _distance: number = 0; 

    constructor(public navCtrl: NavController, private event: Events) { 
    } 

    getDistance() { 
    return new Promise<number>((resolve, reject) => { 
     AMapPlugin.getDistance(data => { 
     resolve(data); 
     }); 
    }); 
    } 

    location() { 
    AMapPlugin.startLocation(10, success => { 
     let lo = { lat: success.latitude, lng: success.longitude }; 
     this.gps = JSON.stringify(lo); 
    }, error => { 
    }); 
    } 

    start() { 
    this.getDistance().then(data => { 
     this._distance = data; 
    }); 
    } 
} 

場所は、GPS時間あたり10秒をトリガしますが、htmlの値は変更し、リアルタイムではありません。

<ion-item> 
    <ion-label>distance</ion-label> 
    <ion-input type="text" [(ngModel)]="_distance" readonly></ion-input> 
    </ion-item> 
    <ion-item> 
    <ion-label>current</ion-label> 
    <ion-input type="text" [(ngModel)]="gps | async" readonly></ion-input> 
    </ion-item> 
+0

gpsは文字列です。約束または観測可能ではありません。「async」の必要はありません。また、 'location()'はどこにありますか? –

+0

'getDistance()'はPromise(一度だけ読み込み)です。新しい価値観を読むためには、Observableを提供するべきです。 –

答えて

0

変更を手動で検出する必要があります。 ngZoneまたは別の角度の変化検出器を使用してください。answer

+0

ありがとうございます、それは動作します –