send multiple parameters when close a modalInstance

Problem

I have a angular project with modals, and I want send multiple (two) parameters when I close, the problem is the second argument it gives undefined, my code is the following

/*Modal controller*/
angular.module('assetModule')
  .controller('assetModalLocationCtrl', ['locationRESTService','$modalInstance','$scope', '$q', assetModalLocationCtrl]);

    function assetModalLocationCtrl (locationRESTService, $modalInstance, $scope, $q) {  

    this.ok = function () {   
      $scope.info.selected = selected ($scope.data.locations)
      $modalInstance.close($scope.data.locations, $scope.info.selected);      
    };
}

/*Controller that invoke the modal*/
this.modalLocation = function modalLocation (size){
    var modalInstance = $modal.open({
      animation: this.animationsEnabled,
      templateUrl: 'views/asset/assetLocationModal.html',
      controller: 'assetModalLocationCtrl',
      controllerAs: 'modalLocation',
      size: size,

    });

    modalInstance.result.then(function (selectedItem, locationList) {
     console.log(selectedItem);
     console.log(locationList);
    }, function () {
      console.log('Modal dismissed at: ' + new Date());
    });
}
Problem courtesy of: oriaj

Solution

You cannot do that as a promise resolves to only one value, you can combine them to an object.

 $modalInstance.close({locations: $scope.data.locations, selected: $scope.info.selected});  

and read them accordingly when the promise is resolved.

modalInstance.result.then(function (transferData) {
   console.log(transferData.selected);
   console.log(transferData.locations);
}, function () {
    console.log('Modal dismissed at: ' + new Date());
});

Side Note:

With Typescript or Babel or any other ES6 polyfill you could make use of object destructuring syntax and write is as:

modalInstance.result.then({locations, selected}) => {
   console.log(locations);
   console.log(selected);
}
Solution courtesy of: PSL

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.