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

share|improve this question
    
so do u have any errors with your code? i think you are trying to remove dup's in your array – Sa E Chowdary Nov 30 '16 at 12:07
    
Did you tried with delete $scope.Profile.address ? – manzapanza Nov 30 '16 at 12:10
    
i want delete one object from my array i am showing html – Adeel Khan Nov 30 '16 at 12:12
    
Use splice , myArray.splice(0, 1) – tom moc Nov 30 '16 at 12:14
    
yes i was use but its not working – Adeel Khan Nov 30 '16 at 12:15
up vote 1 down vote accepted

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======================^

share|improve this answer
    
Thanks its working :) – Adeel Khan Nov 30 '16 at 12:22

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()).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.