我是 Angular 和 nedb 的新手。我正在尝试使用 nedb 中的数组进行 ng-repeat。 我不明白为什么我的代码不起作用
<div ng-repeat="hello in helloworld"></div>
...
hellodb.find({}).sort({helloworld: 1}).exec(function (err, docs){
$scope.helloworld = docs;
console.log($scope.helloworld);
});
如果我对一个与数据库内容相同的 json 文件执行相同的操作
$http.get('helloworld.json').success(function(data) {
$scope.helloworld = data;
console.log($scope.helloworld);
});
控制台中的输出是相同的,并且 ng-repeat 有效
最佳答案
您是否尝试过使用$scope.$apply()
?当您调用一些经典的 Angular 异步函数(例如 $http.get()
)时,最后会自动调用 $scope.$apply()
。我猜这就是它只在你的第二个例子中起作用而不是在第一个例子中起作用的原因。尝试在回调中分配 $scope.helloworld
之后添加它。阅读 this了解更多信息。
关于javascript - 使用 nedb 数组进行 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526554/