如何在窗口侦听器函数中调用控制器函数

时间:2017-11-20 16:07:36

标签: javascript angularjs node.js addeventlistener fs

我希望从window.onbeforeunload激活关闭功能,但是当我按'right click' -> 'close window'关闭应用时,this.close()未启用{我认为由于范围问题)。

侦听器旨在处理应用程序无法正常关闭的情况,但每次关闭应用程序时都需要执行一些操作

this.close = function () {
    var _PACKAGES_FOLDER = process.env.TEMP + '/dmv/packages';
    this.rmDir(_PACKAGES_FOLDER, false);
    DataFolder.createFolder('packages');
    win.close();
}

window.onbeforeunload = function(){  
    this.close()
}

任何解决方案?

2 个答案:

答案 0 :(得分:1)

更新: process.env是node.js变量。您无法在客户端(服务方)使用它。

此外,您需要return某些内容才能使用onbeforeunload功能,您可以根据自己的情况return null;进行操作。

像这样:

window.onbeforeunload = function(){  
  this.close();
  return null;
}

答案 1 :(得分:0)

js code:

(function () {
    dmvMainController.$inject = ['DataFolder','$rootScope','$window'];
    function dmvMainController(DataFolder,$rootScope,$window) {
    // Starts maximized by default (set by main.js)
    var maximized = true;
    var fs = require('fs'),
    path = require('path'),
    filePath = path.join(__dirname,'/../','/../config.json');
    this.configFile = JSON.parse(fs.readFileSync(filePath, 'utf8'));


    // Remove packages folder
    this.rmDir = function (dirPath) {
        var fs = require('fs');
        try { var files = fs.readdirSync(dirPath); }
        catch (e) { return; }
        if (files.length > 0)
            for (var i = 0; i < files.length; i++) {
                var filePath = dirPath + '/' + files[i];
                if (fs.statSync(filePath).isFile())
                    fs.unlinkSync(filePath);
                else
                    this.rmDir(filePath, false);
            }
        fs.rmdirSync(dirPath);
    };

    this.close = function () {
        var _PACKAGES_FOLDER = process.env.TEMP + '/dmv/packages';
        this.rmDir(_PACKAGES_FOLDER, false);
        DataFolder.createFolder('packages');
        win.close();
    } 

    $window.onbeforeunload = function(){
    this.close();
  }
}

angular.module('dmv.core.components').
    component('dmvMain', {
        templateUrl: 'app/views/dmvMain.view.html',
        controller: dmvMainController,
    });

})();

HTML:

<md-toolbar class="root-container">
    <div id="dmv-toolbar" class="md-toolbar-tools no-selectable" layout="row">

        <p class="no-selectable" style="font-size: 16px;">DIGITAL MANUFACTURING <strong>VIEWER</strong></p>

        <div class="draggable" flex></div>

         <md-icon class="siemens-logo" md-svg-src="app/assets/siemens_logo.svg" ng-if="$ctrl.configFile.showSiemensLogo === true"></md-icon>
        <span>
            <md-button class="md-icon-button no-outline" aria-label="Minimize" ng-click="$ctrl.minimize()">
                <md-icon class="control s-18" md-svg-src="app/assets/icon_minimize.svg"></md-icon>
            </md-button>

            <md-button class="md-icon-button no-outline" aria-label="Maximize" ng-click="$ctrl.maximize()">
                <md-icon class="control s-18" md-svg-src="app/assets/icon_maximize.svg"></md-icon>
            </md-button>

            <md-button class="md-icon-button no-outline" id="yourcontainer" aria-label="Close" ng-click="$ctrl.close()">
                <md-icon class="control s-18" md-svg-src="app/assets/icon_close.svg"></md-icon>
            </md-button>
        </span>
    </div>
</md-toolbar>

<tabs-container flex layout="column"></tabs-container>