Dani Grosu
Dani Grosu

Reputation: 554

Develop Desktop App With Electron And Angular 2

I have a problem developing (creating the environment of) a desktop application using Electron and Angular. This app, at some point, has to fire a showOpenDialog which is specific to Electron in order to select a directory from the File System. Let's say I have the following angular component:

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: `
        <button (click)="openDirectory()">Open directory</button>
        <p> {{selectedDirectory}} </p>
    `
})

export class AppComponent {

    selectedDirectory: string = "None";
    openDirectory() {
        // Here I need to call the 'showOpenDialog' from electron
        // and update the selectedDirectory property
    }

}

... but calling showOpenDialog in openDirectory won't work and it will return a compilation error because Angular doesn't have a clue who showOpenDialog really is. In a tipical Electron application you will write something like:

const {dialog} = require('electron').remote;

var path = dialog.showOpenDialog({
    properties: ['openDirectory']
});

Now I have to build the Angular project, modify the bundle.js file and finally add the electron functionalities like showOpenDialog and so on. After that I need to copy the distribution files into Electron project. I know, this is a really mess.
Q: What would be a better way to develop this kind of application? Like both Angular and Electron environments in the same project.

Upvotes: 0

Views: 202

Answers (1)

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11388

You simply need to install electron (and its types) within your angular app, then import your dependency

Installation:

npm install electron @types/electron 

usage:

import { remote } from 'electron';


openDirectory() {
    remote.showOpenDialog();
}

Upvotes: 1

Related Questions