AngularJS: Deleting from scope without hardociding -
i have array of items bound <li>
elements in <ul>
angularjs. want able click "remove item" next each of them , have item removed.
this answer on stackoverflow allows that, because name of array elements being deleted hardcoded not usable across lists.
you can see example here on jsfiddle set up, if try clicking "remove" next game, student removed, not game.
passing this
button gives me access angular $scope @ point, don't know how cleanly remove item parent array.
i have button defined ng-click="remove('games',this)"
, have function this:
$scope.remove = function (arrayname, scope) { scope.$parent[arrayname].splice(scope.$index,1); }
(like jsfiddle) naming parent array while i'm inside seems way break functionality when edit code in year.
any ideas?
i did not why trying pass this
.. never need deal this
in angular. ( , think 1 of strengths! ).
here fiddle solves problem in different way.
the controller simplified
function variousthingsctrl($scope) { $scope.students = students; $scope.games = games; $scope.remove = function (arrayname,$index) { $scope[arrayname].splice($index,1); } }
instead of passing whole scope
, why not pass $index
? since in scope arrays located, should pretty easy then.