Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an Electron dialog from angular 2 component?

I want to click a button to open folder dialog in my component. Here is what I am trying to do:

HTML:

<div>
    <input class="u-full-width" placeholder="Folder" type="text" [(ngModel)]="folder">
    <button id="browse" class="button-primary" (click)="browse()">Browse</button>
    <input id="fileInput" type="file" style="display: none" />
</div>

component.ts

// var remote = require('remote');
// var dialog = remote.require('dialog');

  folder: string;
  browse() {
    dialog.showOpenDialog({title: 'Select a folder', properties: ['openDirectory']}, (folderPath) => {
        if (folderPath === undefined){
            console.log("You didn't select a folder");
            return;
        }
        this.folder = folderPath;
    });
  }

So, how do I import remote and dialog?

like image 577
newman Avatar asked Apr 13 '17 18:04

newman


2 Answers

Just import the "remote" module using es6 import,and then your ts file will be like

import { remote } from 'electron';

folder: string;
browse() {
    remote.dialog.showOpenDialog({title: 'Select a folder', properties: ['openDirectory']}, (folderPath) => {
        if (folderPath === undefined){
            console.log("You didn't select a folder");
            return;
        }
        this.folder = folderPath;
    });
  }
like image 137
Gurjeet singh Avatar answered Oct 16 '22 10:10

Gurjeet singh


You can try it with the ngx-electron library

import {ElectronService} from 'ngx-electron'

export class DialogHelper {

    private static alert = new ElectronService().remote.dialog;
}
like image 2
Shobhit Sharma Avatar answered Oct 16 '22 10:10

Shobhit Sharma