[英]How to copy values from one array of objects to another array of objects in typescript, angular 4?
[英]Angular interpolation lags rendering some values & how to remove one item from the array of typescript objects
我正在使用ASP.NET Core-Angular Web应用程序,在这里我可以选择客户作为来源,并使用Google Maps距离矩阵服务查找与他人的距离和持续时间 。 我得到的值很好( 我认为控制台日志证明滞后不是由于google maps API调用/从对象数组访问值 ),但是当我使用角度插值将数据绑定到模板时,它滞后于渲染距离和持续时间值。 另外要提一下,该表仅在单击按钮后才显示isFindButtonClicked
变为true
。
customers: Array<Customer> = [];
nearbyCustomers: Array<Customer> = [];
getCustomers() {
this.customerService.getAll()
.subscribe( result => {
this.customers = result;
}, error => {
this.alertService.error(error._body);
this.router.navigate(['/home']);
});
}
ngOnInit() {
this.getCustomers();
}
Typescript函数,用于查找和分配距离/持续时间值:
findNearbyCustomers(selectedId: number) {
this.isFindButtonClicked = true;
this.nearbyCustomers = this.customers;
var origin = this.customers.find(c => c.id == selectedId);
var originLatLng = origin.latitude.toString()+","+origin.longitude.toString();
var service = new google.maps.DistanceMatrixService();
for (let x = 0; x < this.customers.length; x++) {
console.log(this.customers[x].id);
var destinationLatLng = this.customers[x].latitude.toString()+","+this.customers[x].longitude.toString();
service.getDistanceMatrix({
origins: [originLatLng],
destinations: [destinationLatLng],
travelMode: google.maps.TravelMode.DRIVING
}, (response, status) => {
if (status.toString() == 'OK') {
console.log('Distance: '+response.rows[0].elements[0].distance.text+', Duration: '+response.rows[0].elements[0].duration.text);
this.nearbyCustomers[x].distance = response.rows[0].elements[0].distance.text;
this.nearbyCustomers[x].duration = response.rows[0].elements[0].duration.text;
console.log('DURATION: '+this.nearbyCustomers[x].duration+', DISTANCE:'+this.nearbyCustomers[x].distance);
}
})
}
}
角度模板的部分标记:
<tbody>
<tr *ngFor="let nearbyCustomer of nearbyCustomers">
<td>{{ nearbyCustomer.businessName }}</td>
<td>
<p>{{ nearbyCustomer.streetNumber + ' ' + nearbyCustomer.streetName + ', ' + nearbyCustomer.suburb }}</p>
<p>{{ nearbyCustomer.city + ' ' + nearbyCustomer.postCode + ', ' + nearbyCustomer.country }}</p>
</td>
<td>{{ nearbyCustomer.contactPerson }}</td>
<td>{{ nearbyCustomer.contactNumber }}</td>
<td>{{ nearbyCustomer.email }}</td>
<td>{{ nearbyCustomer.distance }}</td>
<td>{{ nearbyCustomer.duration }}</td>
</tr>
</tbody>
if (this.customers[x].id != this.selectedDeliveryDestinationId)
并仅推送不包括所选客户(作为来源)的客户this.nearbyCustomers.push(this.customers[x]);
然后,当我尝试为对象属性分配值并进行渲染时,它给我Cannot set property 'distance' of undefined
错误的Cannot set property 'distance' of undefined
-似乎我在弄乱索引值。 为此目的,从数组中排除一项的最佳方法是什么? 或者,如果我只是可以从HTML隐藏一个特定的行,那么它也可以工作。 谢谢。
在这种情况下,您需要ngZone来更新表。
import { NgZone } from '@angular/core';
在构造函数中添加: private _ngZone: NgZone
然后在您的getDistanceMatrix中成功:
this._ngZone.run(() => {
this.nearbyCustomers[x].distance = response.rows[0].elements[0].distance.text;
this.nearbyCustomers[x].duration = response.rows[0].elements[0].duration.text;
});
关于第二个问题,我待会再检查并编辑答案
感谢您的回答。 NgZone帮助解决了变更检测滞后问题。 对于我的问题的第二部分,这是我的解决方法(不确定这是否是最佳解决方案,但对我来说效果很好)
我已经过滤了客户,仅分配了价值,除了选定的价值,像这样: this.nearbyCustomers = this.customers.filter(c => c.id != this.selectedDeliveryDestinationId);
。 这样,我只需要遍历不包括起点的附近客户数组-在每个循环中节省一个API调用浪费:-)
findNearbyCustomers() {
this.isFindButtonClicked = true;
this.nearbyCustomers = this.customers.filter(c => c.id != this.selectedDeliveryDestinationId);
var origin = this.customers.find(c => c.id == this.selectedDeliveryDestinationId);
var originLatLng = origin.latitude.toString()+","+origin.longitude.toString();
var service = new google.maps.DistanceMatrixService();
for (let x = 0; x < this.nearbyCustomers.length; x++) {
var destinationLatLng = this.nearbyCustomers[x].latitude.toString()+","+this.nearbyCustomers[x].longitude.toString();
service.getDistanceMatrix({
origins: [originLatLng],
destinations: [destinationLatLng],
travelMode: google.maps.TravelMode.DRIVING
}, (response, status) => {
if (status.toString() == 'OK') {
this._ngZone.run(() => {
this.nearbyCustomers[x].distance = response.rows[0].elements[0].distance.text;
this.nearbyCustomers[x].duration = response.rows[0].elements[0].duration.text;
})
}
})
}
}
我建议使用管道过滤当前选定的附近的客户。
<tbody>
<tr *ngFor="let nearbyCustomer of nearbyCustomers | nearbyCustomerFilter">
<td>{{ nearbyCustomer.businessName }}</td>
<td>
<p>{{ nearbyCustomer.streetNumber + ' ' + nearbyCustomer.streetName + ', ' + nearbyCustomer.suburb }}</p>
<p>{{ nearbyCustomer.city + ' ' + nearbyCustomer.postCode + ', ' + nearbyCustomer.country }}</p>
</td>
<td>{{ nearbyCustomer.contactPerson }}</td>
<td>{{ nearbyCustomer.contactNumber }}</td>
<td>{{ nearbyCustomer.email }}</td>
<td>{{ nearbyCustomer.distance }}</td>
<td>{{ nearbyCustomer.duration }}</td>
</tr>
</tbody>
创建一个服务,该服务向管道提供有关当前选定的邻近客户的信息。
然后,每当您选择另一个客户时,您的列表都会被更新,并且管道会将特定条目踢出当前视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.