0

How to remove one object from $scope.Profile.address object please help me see below code and image

<tr ng-repeat="x in Profile.addresses">
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td>
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td>
    <td><a href="" data-toggle="tooltip" title="Remove Address" ng-click="removeAddress(x.addresses)"><i class="fa fa-close text-danger" aria-hidden="true"></i></a></td>
</tr>

JS

$scope.removeAddress = function(address) {

    var index = $scope.Profile.addresses.indexOf(address);

    if (index != -1)
      $scope.Profile.addresses.splice(index, 1);
    console.log($scope.Profile.addresses);
};

I have 3 objects and I want to remove one please help me enter image description here

6
  • so do u have any errors with your code? i think you are trying to remove dup's in your array Commented Nov 30, 2016 at 12:07
  • Did you tried with delete $scope.Profile.address ? Commented Nov 30, 2016 at 12:10
  • i want delete one object from my array i am showing html Commented Nov 30, 2016 at 12:12
  • Use splice , myArray.splice(0, 1) Commented Nov 30, 2016 at 12:14
  • yes i was use but its not working Commented Nov 30, 2016 at 12:15

2 Answers 2

1

I think your code is working fine. You are missing something else in your code. Make sure you are passing variables properly from UI.

  <ul ng-repeat="add in Profile.addresses">
    <li ng-click="removeAddress(add)">
      {{add.address}}:{{add.phone}}
    </li>
  </ul>

Have a look here Link

It should be ng-click="removeAddress(x)" not ng-click="removeAddress(x.addresses)"

change======================^

Sign up to request clarification or add additional context in comments.

Comments

0

Just for the heck of it, instead of splicing out the variable from the array, try creating and setting a new array.

So instead of the "find index then splice" in-place array edit, do something like this:

// newArray will contain all objects except the one you want removed
var newArray = $scope.profile.addresses.filter ( function ( d ) {
    return d.address !== address;
});

// newArray should trace out as you'd expect, unless filtering didn't find a match
console.log ( newArray );
// If all is well, replace the old array
$scope.profile.addresses = newArray;
// $scope.profile.addresses should be the same as newArray
console.log ( $scope.profile.addresses )

Now if THAT doesn't work, or it does work but the UI doesn't seem to be updating, then your problem might be applying scope ($scope.$apply()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.