2

I am using nodeWebKit to create an app that uses folders.

I need to get the path of the folder, but the user should select which paths.

The nw API tells to use: nwdirectory

Doing something like below will not work

<input type="file" ng-model="dirPath" nwdirectory />

Well:

<form ng-submit="addDir()">
  <input type="file" ng-model="dirPath" nwdirectory />
  <input class="btn-primary" type="submit" value="add">
</form>

I wanted to pass it to:

$scope.dir = []
$scope.addDir = function() {
  $scope.dir.push({ path: $scope.dirPath });
}

Of course it always shows nothing. How to do this properly?

majidarif
  • 18,694
  • 16
  • 88
  • 133

2 Answers2

0

finally I solved this issue... The problem: there's no binding support for file upload control (read here). So I fixed it like they did:

<input type="file" id="fileDialog" onchange="angular.element(this).scope().setDirectoryPath(this)" nwdirectory />

And added a function in my controller:

$scope.setDirectoryPath = function(value) {
   console.log('path:', value.files[0].path);
};

Maybe there are other (more beautiful) ways to solve it like using your own directive, but this was the fastest working way for me =)!

Regards

Community
  • 1
  • 1
novas1r1
  • 1,803
  • 21
  • 28
0

I created directive for this:

.directive('nwdirectory', ['$timeout', function ($timeout) {
  return {
    restrict: "A",
    scope: {
      directoryPath: "=nwdirectory"
    },
    link: function (scope, element) {

      element.val(scope.directoryPath);
      element.data('old-value', scope.directoryPath);

      scope.$watch('directoryPath', function (val) {
        element.val(scope.directoryPath);
      });

      element.bind('change', function (blurEvent) {
        if (element.data('old-value') != element.val()) {
          $timeout(function () {
            scope.directoryPath = element.val();
            element.data('old-value', element.val());
          });
        }
      });
    }
  };
}])

use it like this:

//controller.js
$scope.directoryPath = "C:\\Users\\Username" //optional

//HTML
<input type="file" nwdirectory="directoryPath">
Dread Boy
  • 772
  • 6
  • 28