我已经阅读了很多关于它的文章,但仍然无法理解我的代码到底出了什么问题。我有两个控制器-一个为每个人的订单收集产品,另一个展示带有订购产品的桌子。显然,我想让他们分享有关订单/产品的数据。不幸的是,在添加新产品后,我无法对orders表进行更新。
export class OrderReviewController {
...
getProducts = () => {
this.orderDataFactory.setOrders(this.orderService.getOrdersPerAppointment());
return this.orderDataFactory.getProducts();
}
}
export class ProductController {
...
refreshOrdersTable = () => {
this.ordersData.setOrders(this.orderService.getOrdersPerAppointment());
this.ordersData.setProducts(undefined);
}
}
export class OrdersData implements interfaces.IOrdersDataFactory {
orders: interfaces.IOrder[];
products: interfaces.ISalesOrderDetail[];
getOrders = () => {
return this.orders;
}
setOrders = (orders: interfaces.IOrder[]) => {
this.orders = orders;
}
getProducts = () => {
if (!this.products){
this.products = [];
for (var i = 0; i < this.orders.length; i++) {
this.products = this.products.concat(this.orders[i].products);
}
}
return this.products;
}
setProducts = (products:any) => {
this.products = products;
}
}
`<tr ng-repeat="sample in oc.getProducts()">
<td><a href="" ng-dblclick="oc.openOrderPopup(sample.SalesOrderId.Id)">{{product.salesordername}}</a></td>
<td><a href="" ng-dblclick="oc.openContactPopup(sample.cdss_contactid.Id)">{{product.customerName}}</a></td>
<td>{{product.title}}</td>
<td>{{product.subject}}</td>
<td>{{product.type}}</td>
<td>{{product.sent ? "yes" : "no"}}</td>
</tr>`不幸的是,我得到了:
错误:达到$rootScope:infdig 10 $digest()迭代。流产!
发布于 2016-12-06 12:47:01
问题存在于getProducts()方法中的OrdersData类中。问题是,每次执行函数时都会创建一个新数组。您不应该每次都重新创建产品数组,而是使用一些更新准则来定义何时创建一个新数组。
如果orders数组与上次相同,则返回相同的对象。
getProducts = () => {
if(!this.products || someUpdateCriteria) {
this.products = [];
for (var i = 0; i < this.orders.length; i++) {
this.products = this.products.concat(this.orders[i].products);
}
}
return this.products;
}有关更多信息,请参见本文:https://docs.angularjs.org/error/$rootScope/infdig
https://stackoverflow.com/questions/40992027
复制相似问题