Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send multiple parameters when close a modalInstance

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());
    });
}
like image 502
oriaj Avatar asked Oct 21 '15 20:10

oriaj


Video Answer


1 Answers

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);
}
like image 119
PSL Avatar answered Oct 20 '22 20:10

PSL