2016-12-02 24 views
0

我要提交給形式ng-repeatinput text - 但錯誤,我提交提交表單NG重複了NG-模型angular.js

HTML:

//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:

這裏是JS for controll呃angular.js

+2

不清楚你面臨什麼問題?請添加更多信息。 – VadimB

+0

添加您遇到的錯誤類型。 – Anbarasan

回答

1

我已經通過了項目進入它保存到另一個範圍變量,然後它在其中呼籲addCart功能訪問的NG-點擊功能NG單擊,

要做的變化:

在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>

+0

感謝兄弟,你救了我的直播:D –

+0

很高興幫助:)如果解決方案對你有幫助,請不要忘記將它標記爲答案,因爲它會幫助稍後檢查此類問題的人 – GraveyardQueen

+0

oke done bro:D –