'Delete an item(Object) from an array using ng-if
I am trying to remove item using ng-if and ng-else-if. "ng-if="navItem.gbn.indexOf('12345') !== -1" is true then array item only show contains('12345') items. but the item still remain in array so I think ng-else-if is not working. I don't know how to delete it.
I have the following Angular code:
<div ng-controller="PIDAEController">
<div class="container-fluid" style="padding-top:30px;">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 tubmbimg" ng-repeat="navItem in navItems | Fpage: curPage * pageSize | limitTo:pageSize" ng-if="navItem.gbn.indexOf('12345') !== -1">
<a class="thumbnail border" href="#/item/{{navItem.site}}">
<div class="b-marjin">
<span align="center" class="label label-primary full-width" >{{ navItem.gbn }}</span>
</div>
<div>
<img style="border: 1px solid black;" class="img-responsive full-width" src="{{navItem.pimg}}" alt="">
</div>
<div align="center" class="under-block">
<span title="{{ navItem.pname }}" align="center">{{ navItem.pname2}}</span>
</div>
</a>
</div>
<div ng-else-if="navItems.splice(navItem, 1)"></div>
</div>
</div>
</div>
angular.forEach(item, function (value, key) {
var keepGoing = true;
if(keepGoing) {
if(value.gbn.indexOf("11600") != -1) {
$scope.initialized = true;
$scope.navItems = item;
$scope.curPage=0;
$scope.pageSize=20;
$scope.numberOfPage = Math.ceil(item.length / $scope.pageSize);
keepGoing = false;
}
else {
$scope.navItems.splice(key, 1);
}
}
});
Solution 1:[1]
ng-else-if does NOT EXIST. its neither angular or angularJS.
In the line
<div ng-else-if="navItems.splice(navItem, 1)"></div>
splice requires navItem to be an index/int, not an object.
As an alternative to ng-else-if you can simply do:
ng-if="navItem.gbn.indexOf('12345') == -1
Also, splice would return the removed array item when found, not sure if "ng-if" handles that as being TRUE.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |