我有一个带有模态的角度项目,我想在关闭时发送多个(两个)参数,问题是它给出了未定义的第二个参数,我的代码如下
/*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());
});
}
答案 0 :(得分:23)
您不能这样做,因为承诺只解析为一个值,您可以将它们组合到一个对象。
$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);
}