我有一个用自己的新控制器打开的模态,我想从模态控制器调用一个函数,但该函数在父控制器中定义。
我将此函数定义为从$ rootScope调用,这是从模态中的父级调用函数的最佳方法,还是将来有意义?
例:
FormModule.controller("formCtrl", function ($scope, $http, $uibModal, $log, $rootScope) {
$rootScope.ShowReport = function ShowReport() {
//function Edit
$scope.edit = function () {
var ObjResolve = function () {
return obj;
}
var modalInstance= $uibModal.open({
animation: true,
templateUrl: 'Modal.html',
controller: 'ModalInstanceCtrl',
resolve: {
ObjResolve
}
}).result.catch(function (res) {
if (!(res === 'cancel' || res === 'escape key press')) {
//throw res;
}
});
};
});
FormModule.controller("ModalInstanceCtrl", function ($scope, $uibModal, $uibModalInstance, $http, ObjResolve, $rootScope ) {
//save Event
$scope.save = function () {
$rootScope.ShowReport();
}
});
您可以使用modalInstance回调,当模态为CLOSED时调用该回调
$scope.edit = function () {
var ObjResolve = function () {
return obj;
}
var modalInstance= $uibModal.open({
animation: true,
templateUrl: 'Modal.html',
controller: 'ModalInstanceCtrl',
resolve: {
ObjResolve
}
}).result.catch(function (res) {
if (!(res === 'cancel' || res === 'escape key press')) {//throw res;}
});
modalInstance.result.then(function(){
ShowReport() //call function when modal is closed
})
};
模态控制器
FormModule.controller("ModalInstanceCtrl", function ($scope, $uibModal, $uibModalInstance, $http, ObjResolve, $rootScope ) {
//save Event
$scope.save = function () {
$uibModalInstance.close();
}
})