为ng-model angular.js提交ng-repeat表格

时间:2016-12-02 09:31:28

标签: javascript php angularjs

我想提交{strong>表格,ng-repeat的{​​{1}}值 - 但我提交的错误

HTML:

input text

JS:

控制器//there is html to view ng-repeat angular.js <form method="post" ng-controller="FoodCtrl" ng-submit="addCart()"> <ion-item class="item-thumbnail-left" > <img ng-src="" ng-click="" > <p style="position:absolute;right:-25px;"> <!--INPUT HIDDEN--> <input type="text" ng-model='x.id'> <input type="text" ng-model='x.menu'> <input type="text" ng-model='x.harga'> <button type="submit" class="button button-balanced button-clear icon ion-android-cart"> </button> </p> <h2> {{x.menu}} </h2> <p>Harga: Rp {{x.harga}}</p> </ion-item> </form>

JS
angular.js

1 个答案:

答案 0 :(得分:1)

我已将该项目传递到ng-click函数,然后将其保存到另一个范围变量中,然后在addCart函数中访问该函数,该函数在ng-click上调用,

要完成的更改:

在HTML中:

<button type="submit" class="button  button-balanced button-clear   icon ion-android-cart" ng-click="saveIndex(x)">  </button> 

在JS中:

$scope.saveIndex=function(x){
$scope.currentItem=x;
}

在addCart函数中:

$scope.addCart=function(){
console.log("id "+$scope.currentItem.id+"menu "+$scope.currentItem.menu+"harga"+$scope.currentItem.harga)


     $http({
     method  : 'POST',
     url     : 'server/menu/add_cart',
     headers : { 'Content-Type' : 'application/json' },
     data    : JSON.stringify({ 
id: $scope.currentItem.id ,
menu: $scope.currentItem.menu, harga:$scope.currentItem.harga })
}).success(function(data) {
    console.log(data);
 });
}

这是一个工作片段,如下所示,

var app = angular.module('TryApp', []);
app.controller('FoodCtrl', function($scope) {
$scope.items=[{id:"232",
 menu:"sdfdsf",
 harga:"adfsdf"
},
{id:"235",
 menu:"sdfdsf",
 harga:"adfsdf"
},
{id:"237",
 menu:"sdfdsf",
 harga:"adfsdf"
},
];
$scope.addCart = function() {

  }
$scope.addCart=function(){
console.log("id "+$scope.currentItem.id+"menu "+$scope.currentItem.menu+"harga"+$scope.currentItem.harga)
                $http({
         method  : 'POST',
         url     : 'server/menu/add_cart',
         headers : { 'Content-Type' : 'application/json' },
         data    : JSON.stringify({ 
id: $scope.currentItem.id ,
menu: $scope.currentItem.menu, harga:$scope.currentItem.harga })
     }).success(function(data) {
        console.log(data);
     });

}
$scope.saveIndex=function(x){
$scope.currentItem=x;
} 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<form method="post" ng-app="TryApp" ng-controller="FoodCtrl" ng-submit="addCart()">
            <div ng-repeat="x in items"> 
                <img  ng-src=""  ng-click="" >
                <p style="position:absolute;right:-25px;">
                <!--INPUT HIDDEN-->
                <input type="text" ng-model='x.id'>
                <input type="text"  ng-model='x.menu'>
                <input type="text" ng-model='x.harga'>

                <button type="submit" class="button  button-balanced button-clear   icon ion-android-cart" ng-click="saveIndex(x)">  </button> 
                </p>

                <h2>  sdfsdf</h2>

                <p>Harga: Rp {{x.harga}} {{currentItem.id}}</p>


            </div>
        </form>