angularjs – 关闭modalInstance时发送多个参数

前端之家收集整理的这篇文章主要介绍了angularjs – 关闭modalInstance时发送多个参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带模态的角度项目,我想在关闭时发送多个(两个)参数,问题是它给出了未定义的第二个参数,我的代码是以下

/*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());
    });
}

解决方法

您不能这样做,因为promise只解析为一个值,您可以将它们组合到一个对象.

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

并在承诺解决后相应地阅读.

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

边注:

使用Typescript或Babel或任何其他ES6 polyfill,您可以使用对象解构语法,并且写为:

modalInstance.result.then({locations,selected}) => {
   console.log(locations);
   console.log(selected);
}

猜你在找的Angularjs相关文章