打字稿:从变量
问题描述:
只打印字符串编码离子框架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>
答
而是节约的距离和名字一起作为一个字符串,你应该为每个“存储”对象。
首先,计算距离不添加名称:
var distRoskilde = ((this.usrStoreLat-this.roskilde.lat) +
(this.usrStoreLong-this.roskilde.long));
然后创建使用对象的数组,像这样:现在
this.storeArray = [{dist: distRoskilde, name: this.rokslide.name},
{dist: distValby, name: this.valby.name}, ...];
,在你的HTML,您可以访问这样的名称:
<p> Tætteste butik: {{this.storeArray[0].name}} </p>
如果您需要将它们一起打印用于记录目的,请执行下列操作:
console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].dist
+ " " + this.storeArray[0].name);
答
public calculateDist(){
var distRoskilde = ((this.usrStoreLat-this.roskilde.lat)+(this.usrStoreLong-this.roskilde.long));
var distValby = ((this.usrStoreLat-this.valby.lat)+(this.usrStoreLong-this.valby.long));
var distKbhK = ((this.usrStoreLat-this.kbhK.lat)+(this.usrStoreLong-this.kbhK.long));
this.storeArray = [{dist: distRoskilde, name: this.roskilde.name}, {dist: distValby, name: this.valby.name}, {dist: distKbhK, name:this.kbhK.name}];
this.storeArray.sort();
console.log('se:' + typeof(this.storeArray[0]) + this.storeArray[0].dist + " " + this.storeArray[0].name);
}
@ggrading所以,现在这一点,对不对?
+0
准确地说,不要忘记在HTML中添加'.name',就像我的例子中看到的一样 – ggradnig
答
我做了什么@ggrading建议,反而使一个variabel用于显示HTML部分,它需要:
nearestStore:串;
this.nearestStore = this.storeArray [0] .name;
in the html:{{nearestStore}}
谢谢!
'console.log(this.storeArray [0] .name);'那是你在问什么? –
我试过了,但它返回“未定义”。但你明白了。这个名字是我想要展示的。 – byblix
我已经尝试在构造函数中包含StoreLocation,所以我可以从商店类中提供名称,但它返回的错误如下所示:“原始异常:没有字符串提供程序” – byblix