Adam Thomason Adam Thomason - 3 months ago 127
AngularJS Question

Blank screen when navigating Angular routes within Electron app

I'm currently writing a desktop hybrid app with Electron with AngularJS integration for routing etc, please see following angular config:

app.config(function($routeProvider, $locationProvider) {

$routeProvider

.when('/', {
templateUrl: 'partials/dashboard.html',
controller: 'dashboardController'
})

.when('/sites', {
templateUrl: 'partials/sites.html',
controller: 'sitesController'
})

.when('/sites/:site', {
templateUrl: 'partials/site.html',
controller: 'siteController'
})

.when('/sites/:site/content', {
templateUrl: 'partials/site_content.html',
controller: 'contentController'
})

.when('/sites/:site/content/create', {
templateUrl: 'partials/site_content_create.html',
controller: 'createController'
})

.when('/sites/:site/content/:contentId/edit', {
templateUrl: 'partials/site_content_edit.html',
controller: 'editController'
})

.when('/user', {
templateUrl: 'patials/user.html',
controller: 'userController'
})

.when('/user/edit', {
templateUrl: 'partials/user_edit.html',
controller: 'userEditController'
})

.when('/login', {
templateUrl: 'partials/login.html',
controller: 'loginController'
})

.when('/register', {
templateUrl: 'partials/register.html',
controller: 'registerController'
});

$routeProvider.otherwise({
redirectTo: '/'
});


});

The app loads up fine, and the initial 'dashboard.html' is injected into ng-view perfectly fine.

The problem comes when I click on an tag to load in another view, such as sites.html, for example. I get a full white screen with no errors output into the console, nor any errors coming from node.js itself.

I'm wondering whether this is a known problem, or whether I've done something wrong in my config.

Answer

I've managed to overcome this issue, finally.

In the end it certainly seemed to be caused by AngularJS being a bit 'funny' about working correctly only when served by a webserver.

With that in mind, I've implemented an express server on a specific port during the 'create window' phase of my electron app. I then point the window at the localhost URL, which is now technically an express app running inside an electron app, running AngularJS.

It confused me for a while getting the setup in line, but now it seems to be working perfectly and very speedy, too.

EDIT: Here's the code for that!

main.js:

const electron = require('electron');
const server = require("./server");
const sqlite3 = require('sqlite3');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 1000, height: 700});

  // and load the index.html of the app.
  //mainWindow.loadURL(`file://${__dirname}/index.html`);
  mainWindow.loadURL(`http://localhost:3333`);

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
});

process.on('uncaughtException', function (err) {
  console.log(err);
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

server.js:

var path = require('path');
var express = require('express');
var app = express();

app.use(express.static(__dirname));

app.get('/', function (req, res) {
    res.sendfile(__dirname + 'index.html');
});

app.listen(3333);
Comments