如何在不刷新页面的情况下自动更新值、ionic2更新值
我的ionic2应用程序从cordova插件获取数据,但输入值尚未自动更新。首先谢谢。 我的演示代码:如何在不刷新页面的情况下自动更新值、ionic2更新值,ionic2,ngmodel,Ionic2,Ngmodel,我的ionic2应用程序从cordova插件获取数据,但输入值尚未自动更新。首先谢谢。 我的演示代码: import { Component } from '@angular/core'; import { NavController, Events } from 'ionic-angular'; declare var AMapPlugin; @Component({ selector: 'page-hello-ionic', templateUrl: 'hello-ionic.h
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;
});
}
}
从'@angular/core'导入{Component};
从“离子角度”导入{NavController,Events};
宣布var AMapPlugin;
@组成部分({
选择器:'page hello ionic',
templateUrl:'hello-ionic.html'
})
导出类HelloInicPage{
gps:string='';
_距离:数字=0;
构造函数(公共navCtrl:NavController,私有事件:事件){
}
getDistance(){
返回新承诺((解决、拒绝)=>{
AMapPlugin.getDistance(数据=>{
解析(数据);
});
});
}
地点(){
AMapPlugin.startLocation(10,成功=>{
设lo={lat:success.latitude,lng:success.longitude};
this.gps=JSON.stringify(lo);
},错误=>{
});
}
开始(){
这个.getDistance().then(数据=>{
这个距离=数据;
});
}
}
位置每次将触发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>
距离
现在的
您必须手动检测更改。使用ngZone
或其他角度变化检测器,检查此gps是否为字符串。。不是承诺或可观察。async
不需要。调用location()
的位置在哪里?getDistance()
是承诺(只读取一次)。为了读取新值,您最好提供一个可观察值。