0

my ionic2 app get data from cordova plugin,but input's value had not update automatic.thank you first. my demo code:

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;
    });
  }
}

location will trigger gps 10 seconds per time,but html value not realtime modify.

  <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>
王志华
  • 15
  • 3

1 Answers1

0

You have to detect the change manually. Use ngZone or another change detector in angular, check this answer

Community
  • 1
  • 1
Jamil
  • 889
  • 8
  • 10