AngularJs doesn't provide it's inbuilt functions to delete an item from an array. So for delete object from the array we have to write our code to delete.
In this post I have implemented functionality to delete an item form an array. Check this out.
Step 1: Add following js code in angular controller.js file.
myApp.controller("itemCtrl", ["$scope", "$state", function ($scope, $state) {
$scope.items = [
{ Id: 1, Name: "name1" },
{ Id: 2, Name: "name2" },
{ Id: 3, Name: "name3" },
{ Id: 4, Name: "name4" }
];
$scope.deleteItem = function(item) {
angular.forEach($scope.vm.items, function (value, index) {
if (item.Id == value.Id) {
$scope.vm.items.splice(index, 1);
}
});
}
}]);
Step 2: Now lets bind that angular array with html view. I have used "ng-click='deleteItem(item)'" to delete an item.
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.Id}}</td>
<td>{{item.Name}}</td>
<td><input type="button" value="Delete" ng-click="deleteItem(item)" /></td>
</tr>
</tbody>
</table>
In this post I have implemented functionality to delete an item form an array. Check this out.
Step 1: Add following js code in angular controller.js file.
myApp.controller("itemCtrl", ["$scope", "$state", function ($scope, $state) {
$scope.items = [
{ Id: 1, Name: "name1" },
{ Id: 2, Name: "name2" },
{ Id: 3, Name: "name3" },
{ Id: 4, Name: "name4" }
];
$scope.deleteItem = function(item) {
angular.forEach($scope.vm.items, function (value, index) {
if (item.Id == value.Id) {
$scope.vm.items.splice(index, 1);
}
});
}
}]);
Step 2: Now lets bind that angular array with html view. I have used "ng-click='deleteItem(item)'" to delete an item.
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td>{{item.Id}}</td>
<td>{{item.Name}}</td>
<td><input type="button" value="Delete" ng-click="deleteItem(item)" /></td>
</tr>
</tbody>
</table>
No comments:
Post a Comment