nw目录作为模型传递给angular
我正在使用nodeWebKit来创build一个使用文件夹的应用程序。
我需要获取文件夹的path,但用户应该select哪些path。
nw API告诉使用: nwdirectory
做下面的事情是行不通的
<input type="file" ng-model="dirPath" nwdirectory />
好:
<form ng-submit="addDir()"> <input type="file" ng-model="dirPath" nwdirectory /> <input class="btn-primary" type="submit" value="add"> </form>
我想把它传递给:
$scope.dir = [] $scope.addDir = function() { $scope.dir.push({ path: $scope.dirPath }); }
当然,它总是什么都不显示。 如何正确地做到这一点?
最后我解决了这个问题…问题:没有绑定支持file upload控制( 在这里阅读)。 所以我像他们那样修正了它:
<input type="file" id="fileDialog" onchange="angular.element(this).scope().setDirectoryPath(this)" nwdirectory />
并在我的控制器中添加了一个function:
$scope.setDirectoryPath = function(value) { console.log('path:', value.files[0].path); };
也许有其他(更漂亮)的方法来解决它,就像使用你自己的指令一样,但是这是对我来说最快的工作方式)!
问候
我为此创build了指令:
.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()); }); } }); } }; }])
像这样使用它:
//controller.js $scope.directoryPath = "C:\\Users\\Username" //optional //HTML <input type="file" nwdirectory="directoryPath">