我有一个列表正在馈送到 View 中,它显示正确,但是当事件触发时,该事件可以将一个项目添加到列表中,并且我希望 Angular 使用列表中的新项目更新 View 。
所以想法是:
.factory('Files', function() {
return service.getFiles()
})
.controller('HomeCtrl', function($scope, Files) {
$scope.files = Files
})
现在使用服务
:
Service.prototype.getFiles = function() {
return this.files
}
以及添加列表的事件:
this.on('created', function(f) {
this.files.push(f)
})
因此,当发出 created
事件时,它会将指向 service.getFiles()< 的工厂添加到文件
应该能够看到数据有新内容。this.files
列表中
Note:
This is all local, the events are done with node.js watching a folder.
This is using Nodewebkit and AngularJS and Node.JS Modules
最佳答案
您所要做的就是监听事件并调用 $scope.$apply
按范围刷新 View 。
.controller('HomeCtrl', function($scope, Files) {
$scope.files = Files.getFiles();
Files.on("created", function(){
$scope.files = Files.getFiles();
$scope.$apply();
});
})
关于javascript - Angular JS 自动更新事件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24486599/