Angular - set dropdown object value upon deleting a dropdown object is not working

15 views Asked by At

Drop-down:

<select class="form-control" id="address_number" name="address_number" ng-model="address" ng-focus="setCurrentAddress(address.number)" ng-options="\'Address \'+ (address.number) for address in addresses track by address.number" ng-change="setCurrentAddress(address.number)"\>

script:

$scope.addresses = [{city: "city1", name: "name1", number: 1},{city: "city2", name: "name2", number: 2}];// array of address objects
  • setCurrentAddress method is used to set all the values with selected address object from dropdown.
$scope.setCurrentAddress = (number) => {
    $scope.address = $scope.addresses.find(address => address.number == number);
};
  • deleteAddress method is used to delet the current selected address, and display the 1st address in the new array.
$scope.deleteAddress = ()=> {
    $scope.addresses = $scope.addresses.filter(address => address.number != $scope.address.number);
    $scope.setCurrentAddress($scope.addresses[0]?.number);
};```



When I try to select Address 1 and delete, it is showing Address 2 as expected.
But, when I am selecting Address 2, deleted Address 2, now instead of showing Address 1 it shows up empty value.


I want to delete Address 2, and I want to see Address 1 displayed in UI intead of empty values.
0

There are 0 answers