javascript - AngularJS remove last element of array in scope -


i have controller in angular contains array , delete method

function($scope, $http){     $scope.arrayofobjects = [];     $scope.remove = function(obj){         var = $scope.arrayofobjects.indexof(obj);         if( > -1 ){             $scope.arrayofobjects.splice(i, 1);         }     } // other things } 

html

<a href ng-repeat="(key, obj) in arrayofobjects track $index">{{obj.id}}  <button type="button" role="button" class="btn btn-default" ng-click="remove(obj)">    <i class="fa fa-trash"></i>    <span>delete</span> </button>  </a> 

now works when delete object other last. when user presses on delete button last object, page gets redirected localhost:3000/# not mapped , blank page.

has encountered such behavior?

while other answers addressing link / redirect issue, solved not having additional clickable items inside anchor tag, bigger problem you're using wrong syntax iterating on objects of array.

to iterate on array want this:

ng-repeat="obj in arrayofobjects" 

the syntax you're using iterating on properties of 1 single object. key , value arguments passed repeater

ng-repeat="(key, value) in object" 

most want this:

<div ng-repeat="obj in arrayofobjects">   {{obj.id}}   <button ng-click="remove(obj)">delete</button> </div> 

codepen


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -