我有這種情況:入門隱藏表單域使用JSON使用列表中AngularJs
我的PHP文件的部分:
<form action="fechar_pedido.php" method="post" enctype="multipart/form-data" ng-controller="carrinhoQtdeController">
<?php
$json_data = array();
foreach($_SESSION['carrinho'] as $key => $value)
{
$json_array = array();
$prod_info = array_values($value);
$json_array += array($prod_info[0] . '-' . $prod_info[1]=>array("id" => $prod_info[0], "tamanho" => $prod_info[1], "qtde" => $prod_info[2], "classificacao" => intval($prod_info[3]), "id_depto" => intval($prod_info[4]),"id_categ" => intval($prod_info[5]),"id_subcateg" => intval($prod_info[6])));
array_push($json_data, $json_array);
}
$json_data = json_encode($json_data);
?>
<input type="hidden" ng-model="jsoncart" value="<?php echo $json_data; ?>" />
上面,我有填充數組與我目前的購物車產品的foreach,並以json格式列表轉換該數組。
之後,我用這個json列表設置了一個隱藏表單域。我的想法是在我的角度函數中獲得這個值,如下所述。
這裏是我的隱藏字段的結果:
<input type="hidden" ng-model="jsoncart" value="[{"1-XGG":{"id":1,"tamanho":"XGG","qtde":4,"classificacao":7,"id_depto":1,"id_categ":1,"id_subcateg":4}},{"2-M":{"id":2,"tamanho":"M","qtde":1,"classificacao":4,"id_depto":1,"id_categ":1,"id_subcateg":4}}]" />
這裏是我的角度文件的一部分:
var app = angular.module('myApp', []);
app.controller('carrinhoQtdeController', function($scope) {
$scope.bill = { total: 0, subtotal: 0 };
$scope.items = "";
$scope.param = "?jsoncart=" + $scope.jsoncart; <----- Here I try to get hidden field value !!!!
$http.get("./fetch_carrinho.php" + $scope.param)
.success(function(response){
$scope.items = response;
})
.error(function(error) {
alert(error);
$scope.carrinho = "error in fetching data";
});
var calculateTotals = function() {
var total = 0;
for (var i = 0, len = $scope.items.length; i < len; i++) {
total = total +
$scope.items[i].price *
$scope.items[i].quantity;
}
$scope.bill.total = total;
$scope.bill.subtotal = total - $scope.bill.discount;
};
});
在線指示,我嘗試加入與jsoncart模型網址,但我無法得到它的價值。在隱藏字段中,我已經使用了「value」,「ng-value」,「ng-bind」,但我無法獲得任何值。
所以,我的問題是如何獲得隱藏的字段值?
謝謝, 馬塞洛。
我想,你的設計方法並不好。無論如何,我假設一些報價問題..改變價值屬性用單引號括起來,並嘗試! – Asik 2015-02-10 18:59:53
我在$ scope.jsoncart中得到了「undefined」,''沒有做任何修改。 順便說一句,我在這裏的接近並沒有完成......它是解決方案的一部分,但我需要解決這個問題。轉發... – Olivertech 2015-02-10 19:29:56
錯誤,您需要學習更多關於AngularJS的知識,對於像您這樣的初學者,我推薦MongoDB,它更簡單 – 2015-02-10 19:52:15