如何更新值auto,ionic2更新值而不刷新页面

时间:2017-05-20 04:06:12

标签: ionic2 ngmodel

my ionic2 app从cordova插件获取数据,但输入的值没有自动更新。先谢谢你。 我的演示代码:

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>

1 个答案:

答案 0 :(得分:0)

您必须手动检测更改。以角度使用ngZone或其他更改检测器,请检查此answer