Inside my ui-bootstrap modal controller, I have a $watch function set up for a variable. The code snippet looks like this:
main.controller('modalCtrl', ['$scope', '$rootScope', '$modalInstance',
function ($scope, $rootScope, $modalInstance) {
var unregister = $rootScope.$watch(function () { return $rootScope.someVariable; },
function (newVal) {
if (newVal == false) {
$scope.closeModal();
}
});
$scope.closeModal = function () {
unregister();
$modalInstance.dismiss('cancel');
};
}]);
When I dismiss the modal, I want to ensure that the $watch is unregistered. This works fine when using ng-click="closeModal()" in the HTML. However, it does not work when dismissing the modal by clicking outside of it or pressing ESC. Is there a way to call the unregister function on dismissal? I am aware of modal.result.then(close(),dismiss()); but I prefer not to include unnecessary functions in the parent $scope.