DRY functions
HTML
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.js"></script>
<table ng-controller="Ctrl">
<thead>
<tr>
<th>
<input ng-model="newItem" type="number">
</th>
<th>
<button type="button" ng-click="addItemDef(newItem)">Add - Array Defined</button>
</th>
<th>
<button type="button" ng-click="addItemUndef(newItem, items)">Add - Array Undefined</button>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.value}}</td>
<td>
<button type="button" ng-click="removeItemDef(item)">Remove - Array Defined</button>
</td>
<td>
<button type="button" ng-click="removeItemUndef(item, items)">Remove - Array Undefined</button>
</td>
</tr>
</tbody>
</table>
JavaScript
function Ctrl($scope) {
$scope.items = [ { value: 5 } ];
$scope.addItemDef = function(item) {
foo = { value: item }
$scope.items.push(foo);
console.log($scope.items)
};
$scope.addItemUndef = function(item, array) {
thing = { value: item }
array.push(thing);
console.log($scope.items)
};
$scope.removeItemDef = function(obj) {
console.log('Before', $scope.items);
$scope.items = _.without($scope.items, obj);
console.log('After', $scope.items);
};
//This is the function that does not work
$scope.removeItemUndef = function(obj, array) {
console.log('Before', array, $scope.items);
var result = _.without(array, obj);
Array.prototype.splice.apply(array, [0, array.length].concat(result));
console.log('After', array, $scope.items);
};
};