angularjs - 从另一个控制器访问 ui-bootstrap 模态解除和关闭功能
angularjs - Accessing ui-bootstrap modal dismiss and close function from another controller
我有模态调用服务,如图所示。
.service('modalService', ['$modal', function ($modal) {
var modalDefaults = {
backdrop: 'static',
keyboard: true,
modalFade: true
};
this.showModal = function (template) {
modalDefaults.template = template;
modalDefaults.controller = 'modalController';
return $modal.open(modalDefaults).result;
};
}])
.controller('modalController', function ($scope, $modalInstance) {
console.log('modalController');
$scope.ok = function () {
$modalInstance.close('okResult');
};
$scope.cancel = function (result) {
$modalInstance.dismiss('cancel');
};
})
我从控制器调用这个模态 'controllerA'。
用于模态的模板包含自定义指令。
我需要在模板的自定义指令控制器的控制器中获得 $modalInstance.dismiss 和 $modalInstance.close 等模态控制器功能,比如 'controllerB'.
我的模板是这样的:
<custom-form></custom-form>
调用另一个控制器的方法有两种方式
1个共享控制器
我们可以在 app.controller
中使用 $controller
共享控制器的方法
2.Sharing rootScope
使用 $rootScope
共享 $rootScope 两个控制器
我有模态调用服务,如图所示。
.service('modalService', ['$modal', function ($modal) {
var modalDefaults = {
backdrop: 'static',
keyboard: true,
modalFade: true
};
this.showModal = function (template) {
modalDefaults.template = template;
modalDefaults.controller = 'modalController';
return $modal.open(modalDefaults).result;
};
}])
.controller('modalController', function ($scope, $modalInstance) {
console.log('modalController');
$scope.ok = function () {
$modalInstance.close('okResult');
};
$scope.cancel = function (result) {
$modalInstance.dismiss('cancel');
};
})
我从控制器调用这个模态 'controllerA'。 用于模态的模板包含自定义指令。 我需要在模板的自定义指令控制器的控制器中获得 $modalInstance.dismiss 和 $modalInstance.close 等模态控制器功能,比如 'controllerB'.
我的模板是这样的:
<custom-form></custom-form>
调用另一个控制器的方法有两种方式
1个共享控制器
我们可以在 app.controller
中使用$controller
共享控制器的方法
2.Sharing rootScope
使用 $rootScope