只打印字符串编码离子框架2.打字稿:从变量
我有一个locationtracker,那是给我一个人的确切经纬度。
我想只有从storeArray打印的字符串,而不是等式中的数字。
如何从变量中忽略数字并只打印商店名称?
this.storeArray = [distValby, distKbhK, distRoskilde];
this.storeArray.sort();
默认排序按最低数量。
相当新的离子2和打字稿。
谢谢!
import { StoreLocation } from '../../providers/store-location';
import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LocationTracker } from '../../providers/location-tracker';
@Component({
selector: 'page-location',
templateUrl: 'location.html'
})
export class LocationPage implements OnInit{
public usrStoreLat: any;
public usrStoreLong: any;
public usrDistance: any;
public storeArray: Array<any> = [];
public roskilde: StoreLocation = new StoreLocation("Bagel Roskilde", "Trekroner st. 4", 55.650107, 12.131997, null);
public valby: StoreLocation = new StoreLocation("Bagel Valby", "Valby Langgade 12", 55.668071, 12.497865, null);
public kbhK: StoreLocation = new StoreLocation("Indre København", "Axeltorv 5, 1609 København V", 55.6887527, 12.5404156, null);
constructor(public navCtrl: NavController, public locationTracker: LocationTracker) {
}
// START GEO TRACKING FOR NUVÆRENDE POSITION
ngOnInit(){
this.start();
}
public start(){
\t this.locationTracker.startTracking();
this.usrStoreLat = this.locationTracker.lat;
this.usrStoreLong = this.locationTracker.lng;
if(this.locationTracker.lat != null){
console.log('Tracker nu!');
console.log('person lokation lat:' + this.usrStoreLat);
console.log('person lokation long:' + this.usrStoreLong);
this.calculateDist();
}
}
public calculateDist(){
var distRoskilde = ((this.usrStoreLat-this.roskilde.lat)+(this.usrStoreLong-this.roskilde.long)) + this.roskilde.name;
var distValby = ((this.usrStoreLat-this.valby.lat)+(this.usrStoreLong-this.valby.long)) + this.valby.name;
var distKbhK = ((this.usrStoreLat-this.kbhK.lat)+(this.usrStoreLong-this.kbhK.long)) + this.kbhK.name;
this.storeArray = [distValby, distKbhK, distRoskilde];
this.storeArray.sort();
console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].toString());
// console.log('store arrayet:' + this.storeArray[0]);
}
stop(){
\t this.locationTracker.stopTracking();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ion-header>
\t <ion-navbar>
\t <ion-title>
\t Location Tracker
\t </ion-title>
\t </ion-navbar>
</ion-header>
<ion-content>
\t <h4>Current Latitude: {{locationTracker.lat}}</h4>
\t <h4>Current Longitude: {{locationTracker.lng}}</h4>
\t \t <p> Tætteste butik: {{this.storeArray[0]}} </p>
\t <button ion-button full primary (click)="start()">Find nærmeste butik</button>
\t <button ion-button full primary (click)="stop()">Stop Tracking</button>
</ion-content>
'console.log(this.storeArray [0] .name);'那是你在问什么? –
我试过了,但它返回“未定义”。但你明白了。这个名字是我想要展示的。 – byblix
我已经尝试在构造函数中包含StoreLocation,所以我可以从商店类中提供名称,但它返回的错误如下所示:“原始异常:没有字符串提供程序” – byblix