2016-04-28 38 views
0

對不起,我的語言能力,希望你能理解所有。Angularjs:輸入ng-repeat元素

我需要爲ng-repeat元素進行輸入,以將輸入數據傳遞給http post。 例如:我有4個元素,我讓輸入的所有元素,和Fe:

elem1- input: 1 
elem2- input: 4 
elem3- input: 1 
elem4- input: 2 

這裏是我的代碼:

.panel-body.note-link ng-repeat=("meal in mealsList track by $index") 
        a data-toggle="tab" 
         h4.pull-right.text-muted 
         | {{meal.price}} zł 

        h4.text-success 
         i.fa.fa-close.text-danger<> ng-click="removeMeal($index)" style="cursor: pointer;font-size: 15px;" 
         | {{meal.name}} {{$index}} 
         input.form-control type="number" ng-model="example" 
         | {{example}} 

,我不知道,如何將輸入數據傳遞給控制器。 任何提示,技巧?

回答

1
angular.module('yourModule').controller('viewController', function($scope) { 
    $scope.mealList = [...]; 
    $scope.example; //The input data will automatically bind to this variable. 
}); 

如果你想輸入你的飯對象中更改數據,那麼這樣做:

input.form-control type="number" ng-model="meal.example" 

然後是一頓對象的屬性值將綁定到輸入

+0

所以,現在,我應該怎麼做,當我想看到的 「榜樣」 輸入,例如,第一頓飯的對象? –

+0

將它附加到「膳食」對象。 然後,當它渲染所有膳食時,每餐都會有自己的「example」輸入綁定到「example」屬性 –

0

只需將其附加到ngModel指令。

<div ng-repeat="item in array"> 
    {{ item.output }} 

    <input ng-model="item.output" /> 
</div> 
1

重複通過您的mealList數組並添加輸入。

例子:https://jsfiddle.net/ptzhL0uL/

控制器

function Controller1($scope) { 
    var vm = this; 

    vm.items_saved = false; 
    vm.mealList = [{ 
    price: '2.99', 
    name: 'Pizza' 
    }, { 
    price: '1.99', 
    name: 'Chips' 
    }, { 
    price: '4.99', 
    name: 'Beer' 
    }]; 

    vm.addNewItem = addNewItem; 
    vm.saveItems = saveItems; 

    function addNewItem() { 
    vm.mealList.push({}); 
    } 

    function saveItems() { 
    vm.items_saved = true; 
    } 
} 

HTML

<button ng-click="ctrl.addNewItem()" class="btn btn-default">Add Item</button> 
<div ng-repeat="item in ctrl.mealList"> 
    <input type="text" ng-model="item.name"> 
    <input type="text" ng-model="item.price"> 
    <input type="number" ng-model="item.quantity"> 
</div> 
<button ng-click="ctrl.saveItems()" class="btn btn-primary">Save</button> 
<hr> 
<div ng-if="ctrl.items_saved"> 
    <h4>Here is your meal list:</h4> 
    <ul> 
    <li ng-repeat="item in ctrl.mealList">{{item.quantity}}{{item.name}} at ${{item.price}} each</li> 
    </ul> 
</div>