例如,如果我在post中使用$ scope.details:
$scope.p_id = 13;
$scope.o_id = 1;
$scope.details = {
quantity: '1',
option: {
p_id: o_id
}
};
我的帖子参数为{p_id:1},但我希望post params为{13:1}。
如果我将值传递为
option: {
[p_id]: o_id
}
现在我按照我的意愿得到了params。在浏览器中,它工作正常,但移动设备显示错误为" Uncaught SyntaxError:意外的令牌["
建议传递密钥值的替代解决方案。
答案 0 :(得分:0)
尝试以下任何一种格式:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.p_id = 13;
$scope.o_id = 1;
$scope.details = {};
$scope.details.quantity = '1';
$scope.details.option = {};
$scope.details.option[$scope.p_id] = $scope.o_id;
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div>p_id: {{p_id}}</div>
<div>o_id: {{o_id}}</div>
<div>option: {{details.option}}</div>
</div>
</body>
</html>
$scope.p_id = 13;
$scope.o_id = 1;
$scope.details = {};
$scope.details.quantity = '1';
$scope.details.option = {};
$scope.details.option[$scope.p_id] = $scope.o_id;
OR
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.p_id = 13;
$scope.o_id = 1;
$scope.details = {
quantity: '1',
option: {
[$scope.p_id]: $scope.o_id
}
};
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div>p_id: {{p_id}}</div>
<div>o_id: {{o_id}}</div>
<div>option: {{details.option}}</div>
</div>
</body>
</html>
$scope.p_id = 13;
$scope.o_id = 1;
$scope.details = {
quantity: '1',
option: {
[$scope.p_id]: $scope.o_id
}
};
两者都有:
$scope.result;
$scope.post = function (details){
return $http({
method: 'POST',
url: //Something,
data: {
details: details
};
});
};
$scope.execute = function () {
$scope.post($scope.details).then(function success(response){
$scope.result = response.data;
});
}
我被告知,最好的一个对移动设备更友好,因为它在压缩Javascript代码后更可靠。
答案 1 :(得分:0)
我尝试使用以下选项传递数据。它有效。
$scope.p_id = 13;
$scope.o_id = 1;
var key = [p_id];
var option = {};
var details = {};
details['quantity'] = '1';
var ids = details.option={};
ids[key] = o_id;
您可以在post方法中传递'详细信息',它在浏览器和移动设备中都可以正常工作。