Tuesday, 15 September 2015

javascript - How to remove an Item from scope AngularJS -



javascript - How to remove an Item from scope AngularJS -

**this drink list, delete button each ingredient.** ![my ingredient page][1] [1]: http://i.stack.imgur.com/qpnhs.png **1. relevant template html:** 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="addnewingredient()") + 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 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

2. relevant controller method:

$scope.removeingredientedit = function (index) { $scope.newdrink.ingredients.splice(index); console.log('removed ingredient @ index ' ); };

when nil happens. using mongodb store drink information. new angular js , not know do.

in template, instead of

ng-click="removeingredientedit(drink)"

try like:

ng-click="removeingredientedit($parent.$index, $index)"

the first parameter, $parent.$index index of drink. $index index of ingredient. can in controller:

$scope.removeingredient = function (i, j) { $scope.drinks[i].ingredients.splice(j, 1) }

javascript angularjs mongodb

No comments:

Post a Comment