2

我在我的webapp中使用angular-ui模式。其中一个模型会显示导航到应用程序各个部分的许多链接(锚点元素)。模态可以像往常一样用modal.dismiss()或modal.close()来关闭。但是当有人通过单击锚点导航出来时,我需要关闭它?通常(不是角度)我可以将一个事件附加到所有锚点,并从那里关闭模态。但角度看起来有点复杂,并不是非常“棱角分明” - 关于如何实现这一点的任何想法/方向?自动关闭导航模式(点击锚点时)

例如:我正在使用angular-ui-router,所以如果这可以通过路由来解决它也可以解决我的问题。

谢谢!

+0

有一个不同的/更好的解决方案类似,但不重复的问题在这里:https://stackoverflow.com/questions/23762323/is-there-a-way-to-automatically-close-angular- ui-bootstrap -modal-when-route-chan?rq = 1 – boatcoder

回答

3

角UI路由器触发事件​​时,发生了状态改变,所以在你的控制器,你可以这样做:

$scope.currentModal = undefined; 

// whenever a modal opens, ensure it is assigned to the $scope.currentModal. Not clear how 
// you are managing your modals at the moment. 

// this event-listener closes the current modal (if any) 
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ 
    if ($scope.currentModal) { 
    $scope.currentModal.dismiss(); 
    } 
}) 
+0

谢谢!我最终把它定位在$ rootscope中,作为开放模式的一个捕获 – alonisser

0

使用$ modalStack.dismissAll()关闭任何modalbox状态变化时。

angular.module('myApp').controller('AppCtrl', ['$scope', '$rootScope', '$modal', '$modalStack', 
    function ($scope, $rootScope, $modal, $modalStack) { 

     $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams, options){ 
      $modalStack.dismissAll(); 
     }); 

    } 
]);