如何将项添加到范围AngularJS

时间:2014-11-08 04:53:03

标签: javascript angularjs node.js mongodb

我是Angular JS和mongod的新手。我希望能够将新的成分字段推入此页面,以便按下+按钮。

这是我的观点

My UI

这是我的.jade文件 block content div.container-fluid(ng-controller="DrinkController", ng-init="setDrinks( #{JSON.stringify(drinks)} )") div.row div.col-md-2 div.col-md-8 h1.title Edit Drinks div.row div.col-md-3 b Name div.col-md-3 b Filename div.col-md-1 div.col-md-5 b Ingredients div.row.editRow(ng-repeat="drink in drinks | orderBy: 'name'") form(ng-submit="editDrink(drink)") div.col-md-3 input(value="{{drink.name}}", ng-model="drink.name") div.col-md-3 input(value="{{drink.image}}", ng-model="drink.image") div.col-md-1.text-right a(ng-click="addNewIngredientEd()") + div.col-md-4 div.row(ng-repeat="ingredient in drink.ingredients") div.col-md-7 select.mixers(value="{{ingredient.name}}", ng-model="ingredient.name", ng-options="i for i in ingredientsList") div.col-md-4 input.addForm.ingredient(value="{{ingredient.amount}}", ng-model="ingredient.amount", style="width: 100px;") div.col-md-1 a(ng-click="removeIngredientEdit($index)") - div.col-md-1 button.btn Save

这是我的相关JS

$scope.addNewIngredientEd = function () {
$scope.newDrink.ingredients.push({ name: '', amount: 0 });

//alert("Hello! I am an alert box!!");
console.log('Added new ingredient Test ');
};

1 个答案:

答案 0 :(得分:0)

您想要$index,它是ng-repeat

的一部分
addNewIngredientEd($index)

$scope.addNewIngredientEd = function ($index) {
   $scope.drinks[$index].ingredients.push(...