Javascript 从工厂更改控制器变量。安格拉斯

Javascript 从工厂更改控制器变量。安格拉斯,javascript,angularjs,Javascript,Angularjs,我发现这个非常好的方法可以捕获角度中的window.onbeforeuload事件。现在我想从它打开我的模态窗口,但我不知道如何才能做到这一点 这是一种捕获方法: examinationApp.factory('beforeUnload', function($rootScope, $window) { // Events are broadcast outside the Scope Lifecycle $window.onbeforeunload = function

我发现这个非常好的方法可以捕获
角度中的
window.onbeforeuload
事件。现在我想从它打开我的模态窗口,但我不知道如何才能做到这一点

这是一种捕获方法:

examinationApp.factory('beforeUnload', function($rootScope, $window) {
    // Events are broadcast outside the Scope Lifecycle
        $window.onbeforeunload = function(e) {
            var confirmation = {};
            var event = $rootScope.$broadcast('onBeforeUnload');
            if (event.defaultPrevented) {
                return confirmation.message;
            }
        };

        $window.onunload = function() {
            $rootScope.$broadcast('onUnload');
        };

        return {};
    })
    .run(function(beforeUnload) {
        // Must invoke the service at least once
    });
    examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http', '$cookieStore', function ($scope, $window, $http, modalDialog) {
        $scope.$on('onBeforeUnload', function (e, confirmation) {
            confirmation.message = confirmation.message = "All data willl be lost.";
            e.preventDefault();
        });
        $scope.$on('onUnload', function (e) {
            console.log('leaving page'); // Use 'Preserve Log' option in Console
        });
在我的控制器中有一个变量
“modalEndShow=false”
。我想在观看活动时切换它

examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http', '$cookieStore', function ($scope, $window, $http, modalDialog) {
modalEndShown=false;}]);

那我该怎么做呢?感谢您的关注。

您可以通过以下操作使模态弹出:

$scope.$on('onBeforeUnload', function (e, confirmation) {
    confirmation.message = confirmation.message = "All data willl be lost.";
    e.preventDefault();
    $scope.$apply(function() {
        $scope.modalEndShown = true;
    });
});

页面上是否加载了
检查Ctrl
控制器?可能是由于加载了控制器而未连接侦听器。是否要在观看事件时切换此选项?这对我来说毫无意义。你到底想干什么?你们能换言之吗?我的html中有一个模态窗口,当conrollers$scope中的变量值为true时,它就会显示出来。当捕捉到onbeforeunload事件时,我想打开我的模式。您是说如果在
$on
方法中执行
$scope.modalEndShown=true
,它将不起作用?谢谢,但我想跳过确认过程。当我用“confirmation.message”擦除字符串并确认时。事件无法捕捉。