【发布时间】:2018-03-30 02:51:12
【问题描述】:
我的 angular4 应用程序中发生了一件非常奇怪的事情,我一生都无法弄清楚。
本质上是我
- 将产品和项目的完整列表中的 (@Input) 加载到名为 products 的对象中。
- 在名为实体的对象中加载 (@Input),该对象包含具有完整产品列表子集的产品属性,即只有用户保存到实体中的产品。
- 加载产品数据 - 我将每个产品从 this.products 推送到 productSelectionData
- 然后我运行一个函数,循环遍历 productSelectionData 中的所有项目,并检查实体对象中是否有任何具有名为 selected 的属性的项目,并将 selected 的值更改为 true
此时一切看起来都很好
- 然后我运行一个函数来拼接 selectedProducts 和选中属性为 false 的项目。
这就是问题发生的地方。由于某些对我来说并不明显的原因,productSelectionData 对象和 selectedProducts 对象都将带有 selected = false 的项目从数组中拼接出来。
代码如下:
import { Component, Input, OnInit } from '@angular/core';
import { ProposalModel, ProductModel } from './../../../shared/models/';
@Component({
selector: 'mj-proposal-edit',
templateUrl: './proposal-edit.component.html',
styleUrls: ['./proposal-edit.component.scss']
})
export class ProposalEditComponent implements OnInit {
@Input() entity: ProposalModel;
@Input() products: ProductModel[];
productSelectionData: any;
selectedProducts: any;
constructor() { }
ngOnInit() {
// Load all products and items
this.loadProductData();
this.updateProductSelectionData();
this.filterProductsSelected();
}
loadProductData() {
this.productSelectionData = [];
this.products.forEach(product => {
this.productSelectionData.push(
{ productTitle: product.productTitle, items: product.items })
});
console.log('Product Selection, after load: ', this.productSelectionData);
debugger;
}
updateProductSelectionData() {
// Update Product Selection Object with previously selected data
// 1. Check if there is previously saved data
if (this.entity.products !== undefined) {
// 2. Update productSelectionData with values saved in entity object
this.productSelectionData.forEach(product => {
if (this.entity.products !== undefined) {
this.entity.products.forEach(entityProduct => {
if (product.productTitle === entityProduct.productTitle) {
if (product.items !== undefined) {
product.items.forEach(item => {
if (entityProduct.items !== undefined) {
entityProduct.items.forEach(entityItem => {
if (item.code === entityItem.code) {
item.selected = true;
item.quantity = entityItem.quantity;
item.discount = entityItem.discount;
}
});
}
});
}
}
});
}
});
console.log('Product Selection, after update: ', this.productSelectionData);
debugger;
}
}
filterProductsSelected() {
this.selectedProducts = [];
this.productSelectionData.forEach(product => {
this.selectedProducts.push(product)
});
this.selectedProducts.forEach(selectedProduct => {
selectedProduct.items.forEach(item => {
const itemIndex = selectedProduct.items.indexOf(item);
if (item.selected === false) {
selectedProduct.items.splice(itemIndex, 1);
}
if (item.selected === undefined) {
selectedProduct.items.splice(itemIndex, 1);
}
});
});
console.log('Selected Products, after filter: ', this.selectedProducts);
console.log('Product Selection, after filter: ', this.productSelectionData);
debugger;
}
}
【问题讨论】:
标签: javascript angular typescript