Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created February 6, 2016 02:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwormald/282f475bc2d88b30fe56 to your computer and use it in GitHub Desktop.
Save robwormald/282f475bc2d88b30fe56 to your computer and use it in GitHub Desktop.
//file service
angular.service('FileDialog', function($q){
//reads file from disk, returns a promise (integrated into the digest cycle)
function readFile(filename){
return $q(function(resolve, reject){
fs.readFile(fileName, 'utf-8', function (err, data){
if(err){
reject(err);
return;
}
resolve(data);
});
});
}
function openFileDialog(){
return $q(function(resolve, reject){
dialog.showOpenDialog(function(filename){
resolve(filename);
});
})
}
this.openFile = function(){
return openFileDialog().then(readFile);
}
});
angular.controller('Foo', function(FileService){
this.openFile = function(){
FileService.openFile().then(function(fileData){
//now do something with file....
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment