I'm writing an Angular app in node-webkit so that I can access the file system and other API's like desktop notifications and whatnot.
I have an Angular service (let's call it FileService
) that uses fs.readdir()
to read a directory of files and pushes the name of each file onto an array called allFiles
as an object. So when this method finishes doing its thing, it returns an array allFiles
of objects with this structure: {name: "some_file_name.txt"}
via a callback.
Here's how the relevant bits of the FileService
code looks like:
// ...
methods.getFileList = function (cb) {
var allFiles = [];
fs.readdir(directory, readFiles);
// @todo: this is ugly as hell. Refactor later.
function readFiles(err, files) {
if (err) throw err;
files.forEach(function (filename) {
if (!isSupportedFileFormat(filename)) return;
fs.open(directory + filename, 'r', function (err, fileDescriptor) {
var buffer = new Buffer(85);
fs.read(fileDescriptor, buffer, 0, buffer.length, null, function (err, bytesRead, buffer) {
allFiles.push({
name: filename,
contents: buffer.toString('utf8', 0, bytesRead)
});
fs.close(fileDescriptor);
});
});
});
cb(allFiles);
}
};
// ...
In my main controller, let's call it MyCtrl
, I have some code similar to this:
// FileService is the Angular factory I wrote that uses Node's fs to get files
FileService.getFileList(function (list) {
$scope.listOfFiles = list;
console.log($scope.listOfFiles);
});
And in my view (let's say it's in index.html
), I have some code like this:
<ul class="file-list">
<li ng-repeat="file in listOfFiles">{{ file.name }}</li>
</ul>
Now, I have everything set up so that the debugging console pops up when the app loads so I can see what's going on. When I build and run the app, I can see in the console a list of all the files showing up nicely in the scope variable listOfFiles
. However, in my index.html
view, I just get a blank area on the screen where the list should be. But once I move the app window or click on a textarea
or even on the empty space where the list should be, the list of files magically pops up like I'd expect it to.
I started grasping at straws, and thought that maybe I needed to trigger a redraw to display my file list. So I tried the suggestions on this site and also some suggestions by Paul Irish, but none of those solutions fixed the problem.
Update: I meant to note that I tried doing
$scope.$apply(function() {
$scope.listOfFiles = list;
}
but sadly that didn't fix the problem either.