【发布时间】:2016-06-03 08:07:05
【问题描述】:
我正在尝试使用 angular bootstrap 来获取模态框。我可以很好地启动模态,但我在解除模态时遇到了一些范围问题。
当我启动模式时,我可以指定一个控制器,我可以从中调用函数,它可以工作,但它似乎是控制器的 副本,没有 $parent 并且没有任何控制器 -局部变量。
我需要访问 $uibModal.open() 的返回值才能关闭模式,所以我试图将它存储在 var modalInstance 中,当我在控制器范围内时它工作正常,但是传递给 $uibModal 服务的控制器副本没有设置局部变量 modalInstance。
我可以通过将返回对象存储在 $rootScope 中来解决这个问题,但这似乎是个坏主意。我错了吗?从传递给 $uibModal 服务的点击处理程序访问 modalInstance 的最佳方法是什么?我可以避免使用 $rootScope 吗?
var app = angular.module('main', ['ui.bootstrap']);
app.controller('MainCtrl', function($scope, $rootScope, $uibModal) {
var modalInstance;
$scope.launch = function() {
console.log('launch');
modalInstance = $uibModal.open({
template: '<div>Modal Content - <a ng-click="close()">Close</a></div>',
controller: 'MainCtrl',
});
// Wouldn't need to do this if I could access modalInstance in close handler
$rootScope.modalInstance = modalInstance;
}
$scope.close = function () {
console.log('close');
console.log(modalInstance);
// Works, but should I be using $rootScope like this?
//$rootScope.modalInstance.close();
// Doesn't work, modalInstance is undefined
modalInstance.close();
}
});
【问题讨论】:
标签: angularjs angular-bootstrap