如何将Javascript数组作为Json与html表单组合发送?
我正在创建一个餐厅菜单应用程序,服务员可以使用它输入订单 我有一个名为itemOrderList的Js数组,用于存储项目名称。我希望能够将项目名称列表作为Json数组发送到后端,并将客户名称表单输入字段和项目价格存储在我的数据库中。我在做这件事时遇到了一些问题。我该怎么办?GoogleDevTools说“ReferenceError:itemOrderList未定义”,我正在尝试对Js数组进行字符串化 AngularJs代码如何将Javascript数组作为Json与html表单组合发送?,javascript,json,angularjs,Javascript,Json,Angularjs,我正在创建一个餐厅菜单应用程序,服务员可以使用它输入订单 我有一个名为itemOrderList的Js数组,用于存储项目名称。我希望能够将项目名称列表作为Json数组发送到后端,并将客户名称表单输入字段和项目价格存储在我的数据库中。我在做这件事时遇到了一些问题。我该怎么办?GoogleDevTools说“ReferenceError:itemOrderList未定义”,我正在尝试对Js数组进行字符串化 AngularJs代码 .controller('orderAddCtrl', ['$sco
.controller('orderAddCtrl', ['$scope', '$location', 'dataService', function ($scope, $location, dataService) {
$scope.itemOrderList = [];
$scope.totalItemPrices = 0;
$scope.addOrderToList = function (item) {
console.log(item.itemName);
$scope.addPricesToTotalItemPrices(item.itemPrice);
$scope.itemOrderList.push(item.itemName);
};
$scope.addPricesToTotalItemPrices = function (price) {
console.log(price);
$scope.totalItemPrices += price ;
};
$scope.removeFromOrderToList = function (index) {
console.log(index);
$scope.itemOrderList.splice(index, 1);
};
$scope.createOrder = function (order) {
var myJson = JSON.stringify(itemOrderList);
order.orderPrice = totalItemPrices;
order.orderItems = myJson;
dataService.addOrder(order).then(function () {
$location.path('/');
});
};
Html
客户名称
总价:${totalItemPrices}
食品
{{i.itemName}
订购项目
-
{{i}}/
去除
我打赌您需要指定您正在使用$scope中声明的变量,以便
$scope.createOrder = function (order) {
var myJson = JSON.stringify($scope.itemOrderList);
order.orderPrice = $scope.totalItemPrices;
order.orderItems = myJson;
dataService.addOrder(order).then(function () {
$location.path('/');
});
};
现在还不清楚您的实际问题是什么。抱歉,它说ReferenceError:itemOrderList没有在我试图字符串化Js数组的地方定义。我不知道为什么它不能看到数组?您不应该在stringify中使用$scope.itemOrderList吗?
$scope.createOrder = function (order) {
var myJson = JSON.stringify($scope.itemOrderList);
order.orderPrice = $scope.totalItemPrices;
order.orderItems = myJson;
dataService.addOrder(order).then(function () {
$location.path('/');
});
};