我正在开发angularjs 1.6.5
我正在使用以下代码发送发票页面的数据。
$scope.products = [{qty: 0, description: ' ', sellingPrice: 0.0, unitPrice: 0.0, total: 0.0}];
$scope.fproducts = [];
$scope.generateInvoice = function () {
console.log("generateInvoice");
console.log($scope.fproducts);
console.log("sub total " + $scope.subTotal + " ft " + $scope.finalTotal + " pamt " + $scope.paidAmount + " baldue " + $scope.balancedue);
$scope.bd1 = {
'subTotal': $scope.subTotal,
'total': $scope.finalTotal,
'TotalPaid': $scope.paidAmount,
'invDateStr': $filter('date')($scope.invoiceDate, "MM/dd/yyyy"),
};
if ($scope.fproducts.length > 0) {
$scope.fproducts.forEach((total, index) => {
Object.entries(total).forEach(([key, value]) => {
console.log(index + " " + key + " " + value);
$scope.bd1[`billProductList[${index}].${key}`] = value;
});
});
}
//commenting above for each and removing comment from below code is
// working properly but I want to send dynamic data with above code
/* $scope.bd1[`billProductList[0].id`] = 1;
$scope.bd1[`billProductList[0].description`] = 1;
$scope.bd1[`billProductList[0].discountPercent`] = 150;
$scope.bd1[`billProductList[0].qty`] = 10;
$scope.bd1[`billProductList[0].sellingPrice`] = 100;
$scope.bd1[`billProductList[0].unitPrice`] = 100;
$scope.bd1[`billProductList[0].total`] = 150;*/
$scope.bd1[`BillPaidDetailses[0].paymentMode`] = $scope.paymentMode;
$scope.bd1[`BillPaidDetailses[0].amount`] = $scope.paidAmount;
console.log($scope.bd1);
$http({
method: 'POST',
url: 'added-invoice',
data: $httpParamSerializerJQLike($scope.bd1),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(data, status, headers, config) {
if (data.data.st === 1) {
$scope.success = true;
window.location = 'bhome#!/show-tax';
} else if (data.data.st === 0) {
$scope.success = false;
$scope.failure = true;
} else if (data.data.st === -1) {
$scope.success = false;
$scope.failure = true;
}
}, function errorCallback(data, status, header, config) {
$scope.success = false;
$scope.failure = true;
});
}
上面的代码没有将值传递给服务器端对象。其console.log值为
关于修改以上数据
/*if ($scope.fproducts.length > 0) { $scope.fproducts.forEach((total, index) => { Object.entries(total).forEach(([key, value]) => { console.log(index + " " + key + " " + value); $scope.bd1[`billProductList[${index}].${key}`] = value; }); }); }*/ $scope.bd1[`billProductList[0].id`] = 1; $scope.bd1[`billProductList[0].description`] = 1; $scope.bd1[`billProductList[0].discountPercent`] = 150; $scope.bd1[`billProductList[0].qty`] = 10; $scope.bd1[`billProductList[0].sellingPrice`] = 100; $scope.bd1[`billProductList[0].unitPrice`] = 100; $scope.bd1[`billProductList[0].total`] = 150;
因为这是将值传递给服务器对象,所以console.log是
我想使用if条件中指定的动态值运行代码。有什么问题我无法解决。
编辑 我正在使用服务器端struts2我的代码是
public class BillNewAction extends ActionSupport implements ModelDriven{ BillDetails bd = new BillDetails(); private List billList = new ArrayList (); public String insert() { System.out.println("total " + bd.getTotal() + " subTotal " + bd.getSubTotal() + " paid " + bd.getTotalPaid() + " invoiceDate " + bd.getInvDateStr() ); SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy"); try { bd.setInvoiceDate((new java.sql.Date(formatter1.parse(bd.getInvDateStr()).getTime()))); } catch (ParseException ex) { Logger.getLogger(BillNewAction.class.getName()).log(Level.SEVERE, null, ex); } for (BillPaidDetails b : bd.getBillPaidDetailses()) { System.out.println("type " + b.getPaymentMode() + " amount " + b.getAmount() ); } System.out.println("product size " + bd.getBillPrList().size()); for (BillProduct b : bd.getBillPrList()) { System.out.println("id " + b.getId() + " desc " + b.getDescription() + " qty " + b.getQty() + " sp " + b.getSellingPrice() + " up " + b.getUnitPrice() + " " + b.getTotal() ); } } }
公共类BillDetails实现java.io.Serializable {
private Long billNo; private Client client; private BigDecimal subTotal; private BigDecimal TotalAmount;//total price private BigDecimal TotalPaid;//Amount paid for getting items private BigDecimal vat; private BigDecimal total; private String invoiceNo; private Date invoiceDate; private String invDateStr; private ListBillPaidDetailses = new ArrayList (); private List billPrList = new ArrayList (); //getter and setter }
我必须以$scope.bd1[billProductList[0].id] = 1;
格式将数据发送到服务器
分配传递数据到服务器的单个价值,但是我没有动态值,所以我正在尝试
if ($scope.fproducts.length > 0) { $scope.fproducts.forEach((total, index) => { Object.entries(total).forEach(([key, value]) => { console.log(index + " " + key + " " + value); $scope.bd1[billProductList[${index}].${key}] = value; }); }); }
那不起作用