title: Trouble With 'Scope' of Angular Controller In Electron App
body: <p>Background: I have a gui I'm putting together for an application I've been working on. The app loads its last state from a YAML file, and is supposed to save that state to the YAML file when the app closes. I'm using the following event listener...</p>
  
  <pre><code>// Handle close event
  window.addEventListener('beforeunload', function(event) {
      if(doc) {
          // This line gives error: "Uncaught ReferenceError: main is not defined"
          doc.project_notes.text = main.summernoteText;
          fs.writeFile('../resources/project.yml', yaml.safeDump(doc), function (error) {
              if (error) {
                  throw error
              }
          });
      }
  
      console.log("Bye...");
  });
  
  angular
      .module('inspinia')
      .controller('MainCtrl', MainCtrl)
  </code></pre>
  
  <p>(and in my index.html)</p>
  
  <pre><code>&lt;body ng-controller="MainCtrl as main"&gt;
  </code></pre>
  
  <p>I'm saving all of the states in a global variable called <code>doc</code>. It loads the yml file just fine using the following</p>
  
  <pre><code>function MainCtrl() {
  
      activeProject = LoadActiveProject();
  
      /**
       * summernoteText - used for Summernote plugin
       */
      this.summernoteText = doc.project_notes.text;
  
  };
  </code></pre>
  
  <p>My issue is primarily with scope of the angular controller/variables. </p>
  
  <p>First, am I handling this correctly in general?
  If this is reasonable, how can I update doc.project_notes.text from my unload function OR link the two variables by reference?</p>
  
  <p>I'm pretty new to Javascript.</p>