I am trying to write a function that enables me to remove an item when the button is clicked but I think I am getting confused with the function - do I use $digest
?
HTML & app.js:
<ul ng-repeat="bday in bdays">
<li>
<span ng-hide="editing" ng-click="editing = true">{{bday.name}} | {{bday.date}}</span>
<form ng-show="editing" ng-submit="editing = false">
<label>Name:</label>
<input type="text" ng-model="bday.name" placeholder="Name" ng-required/>
<label>Date:</label>
<input type="date" ng-model="bday.date" placeholder="Date" ng-required/>
<br/>
<button class="btn" type="submit">Save</button>
<a class="btn" ng-click="remove()">Delete</a>
</form>
</li>
</ul>
$scope.remove = function(){
$scope.newBirthday = $scope.$digest();
};
I usually write in such style :
Hope this will help You have to use a dot(.) between $scope and [yourArray]
Here is another answer. I hope it will help.
Full source is here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
To remove item you need to remove it from array and can pass
bday
item to your remove function in markup. Then in controller look up the index of item and remove from arrayThen in controller:
Angular will automatically detect the change to the
bdays
array and do the update ofng-repeat
DEMO: http://plnkr.co/edit/ZdShIA?p=preview
EDIT: If doing live updates with server would use a service you create using
$resource
to manage the array updates at same time it updates serverThis is a correct answer:
In @charlietfl's answer. I think it's wrong since you pass
$index
as paramter but you use the wish instead in controller. Correct me if I'm wrong :)I disagree that you should be calling a method on your controller. You should be using a service for any actual functionality, and you should be defining directives for any functionality for scalability and modularity, as well as assigning a click event which contains a call to the service which you inject into your directive.
So, for instance, on your HTML...
Then, create a directive...
Then in your service...
When you write your code properly like this, you will make it very easy to write future changes without having to restructure your code. It's organized properly, and you're handling custom click events correctly by binding using custom directives.
For instance, if your client says, "hey, now let's make it call the server and make bread, and then popup a modal." You will be able to easily just go to the service itself without having to add or change any of the HTML, and/or controller method code. If you had just the one line on the controller, you'd eventually need to use a service, for extending the functionality to the heavier lifting the client is asking for.
Also, if you need another 'Delete' button elsewhere, you now have a directive attribute ('ng-remove-birthday') you can easily assign to any element on the page. This now makes it modular and reusable. This will come in handy when dealing with the HEAVY web components paradigm of Angular 2.0. There IS no controller in 2.0. :)
Happy Developing!!!
Using
$index
works perfectly well in basic cases, and @charlietfl's answer is great. But sometimes,$index
isn't enough.Imagine you have a single array, which you're presenting in two different ng-repeat's. One of those ng-repeat's is filtered for objects that have a truthy property, and the other is filtered for a falsy property. Two different filtered arrays are being presented, which derive from a single original array. (Or, if it helps to visualize: perhaps you have a single array of people, and you want one ng-repeat for the women in that array, and another for the men in that same array.) Your goal: delete reliably from the original array, using information from the members of the filtered arrays.
In each of those filtered arrays, $index won't be the index of the item within the original array. It'll be the index in the filtered sub-array. So, you won't be able to tell the person's index in the original
people
array, you'll only know the $index from thewomen
ormen
sub-array. Try to delete using that, and you'll have items disappearing from everywhere except where you wanted. What to do?If you're lucky enough be using a data model includes a unique identifier for each object, then use that instead of $index, to find the object and
splice
it out of the main array. (Use my example below, but with that unique identifier.) But if you're not so lucky?Angular actually augments each item in an ng-repeated array (in the main, original array) with a unique property called
$$hashKey
. You can search the original array for a match on the$$hashKey
of the item you want to delete, and get rid of it that way.Note that
$$hashKey
is an implementation detail, not included in the published API for ng-repeat. They could remove support for that property at any time. But probably not. :-)Invoke with:
EDIT: Using a function like this, which keys on the
$$hashKey
instead of a model-specific property name, also has the significant added advantage of making this function reusable across different models and contexts. Provide it with your array reference, and your item reference, and it should just work.