How to close Angular UI Modal from anywhere

Inject the $modalStack service and call the function $modalStack.dismissAll(), see the code on GitHub for details:

myApp.factory('ModalService', ['$modal', '$modalStack' function($modal, $modalStack) {
    return {
        trigger: function(template) {
            $modal.open({
                templateUrl: template,
                size: 'lg',
                controller: function($scope, $modalInstance) {
                    $scope.ok = function() {
                        $modalInstance.close($scope.selected.item);
                    };
                    $scope.cancel = function() {
                        $modalInstance.dismiss('cancel');
                    };
                }
            });
        },
        close: function(reason) {
            $modalStack.dismissAll(reason);
        }
    };
}]);

I added the below line to prevent browser back button routing and closing the popup. We need to inject $modalStack into angular controller.

event.preventDefault();
            $modalStack.dismissAll('close');