Hi guys I am new to Angular so please be easy. I noticed that when I tried deleting an object from an array passed into a function, it would delete the object from the array within the function, but not in the original array. It confused me because I thought they were passed by reference:
function controller($scope, $http) {
var vm = this;
vm.breakfast = [ { id:1, 'name':'fruit' }, { id:2, 'name':'egg' } ];
function removeFromMeal(meal, entryId) {
meal = meal.filter (function (entry) {
return entry['id'] !== entryId;
}
}
function manageEntry(entry) {
...
// Condition to remove meal from vm.breakfast
if (...) { removeFromMeal(vm.breakfast, entry['id']); }
}
}
The issue is, if I pass an entry with an id of 1 into manageEntryand set breakpoints within removeFromMeal, I can see that the 'meal' array is successfully being filtered (i.e. object with id 1 is removed from vm.breakfast). However, as soon as that function returns, it(vm.breakfast) is exactly the same as if the function never ran. I'm sure the issue is with context/scope, would the use of $apply be apt here somewhere?
Thanks in advance.
deleteorarray.splice(). I don't see anything like that.array.filterreturns a new array, it doesn't modify the given array.