2014-09-27 38 views
0

標題可能有點混淆,但這是我正在嘗試做的。使用一個列表來填充另一個列表並保存第二個附加信息

我有一個列表

$scope.List = [{ 
    "id": 0, 
    "name": "Apples" 
    }, { 
    "id": 1, 
    "name": "Oranges" 
    }, { 
    "id": 2, 
    "name": "Peaches" 
    }, { 
    "id": 3, 
    "name": "Coconuts" 
    }, ]; 

而且我有,所以當我點擊它被添加到列表第二次果,但隨後我能夠指定數量和價格。

我想要做的是將這個第二個列表與金額和價格信息保存到一個數組(稱爲MyList),我100%難倒。

這是我到目前爲止它也是在plnkr任何和所有的幫助,非常感謝。

angular.module('formExample', []) 
    .controller('ExampleController', ['$scope', 
    function($scope) { 
     $scope.List = [{ 
     "id": 0, 
     "name": "Apples" 
     }, { 
     "id": 1, 
     "name": "Oranges" 
     }, { 
     "id": 2, 
     "name": "Peaches" 
     }, { 
     "id": 3, 
     "name": "Coconuts" 
     }, ]; 

     $scope.NewList = []; 

     $scope.MyList = {}; 

     $scope.addfood = function(fruit) { 
     $scope.NewList.push(fruit); 
     $scope.List.splice($scope.List.indexOf(fruit), 1); 
     }; 

     $scope.returnfood = function(newFruit) { 
     $scope.List.push(newFruit); 
     $scope.NewList.splice($scope.NewList.indexOf(newFruit), 1); 
     }; 

     $scope.save = function(Nfood) { 
     $scope.MyList = angular.copy(Nfood); 
     }; 


    } 
    ]); 

下面是HTML

<body ng-app="formExample"> 
    <div ng-controller="ExampleController"> 
    <p ng-repeat="food in List"> 
     <a ng-click="addfood(food)"> 
      {{food.name}} 
     </a> 
    </p> 
    <hr> 
    <div ng-repeat="Nfood in NewList"> 
     {{Nfood.name}} 
     <input ng-model="Nfood.Amount" type="number" class="form-control" min="0"><span class="input-group-addon">Amount</span> 
     <input ng-model="Nfood.Price" type="number" class="form-control" min="0"><span class="input-group-addon">Price</span> 
     <button ng-click="returnfood(Nfood)">REMOVE</button> 
    </div> 
    <button ng-click="save(Nfood)">Save</button> 


    </div> 

    <pre> 
    {{MyList | json}} 
</pre> 

回答

0

工作示例here

在你第一次存儲在您的到位按鈕它無法訪問您的增值經銷商,我的意思是控制器之外,你也應該爲MyList創建數組而不是對象,使用push函數添加元素後

<div ng-repeat="Nfood in NewList"> 
      {{Nfood.name}} 
      <input ng-model="Nfood.Amount" type="number" class="form-control" min="0"><span 
       class="input-group-addon">Amount</span> 
      <input ng-model="Nfood.Price" type="number" class="form-control" min="0"><span 
       class="input-group-addon">Price</span> 
     <button ng-click="returnfood(Nfood)">REMOVE</button> 
     <button ng-click="save(Nfood)">Save</button> 
    </div> 
+0

因此當我運行的工作示例,我沒有看到

 {{MyList}} 
填充任何東西。 – user3271518 2014-09-27 17:15:13

+0

對不起,這是錯誤的鏈接,我已經更新 – 2014-09-27 18:30:06

+0

所以這確實工作,並且沒有回答我的問題。可能是因爲我沒有問好。但有沒有辦法讓save()捕獲整個列表? – user3271518 2014-09-27 22:54:13

相關問題