在angular.js + node.js /电子的承诺不起作用
我在电子中使用angular.js和node-orm来与数据库进行交谈。 Node-orm的find / get函数是asynchronous的,所以我尝试使用Promise来获取数据,如下所示:
app.service('SearchService', function($q) { this.title = function(token) { var deferred = $q.defer(); Unit.find({}).where("unit_title LIKE ?", ['%'+token.toUpperCase()+'%']).run(function(err, results) { if (err) { return console.error('error running title query', err);} deferred.resolve(results); }); return deferred.promise; } }); app.controller("GreetController", function($scope, SearchService) { $scope.units = SearchService.title('test'); });
在angular度上翻译承诺的目标本身。
<div ng-controller="GreetController"> <ul> <li ng-repeat="unit in units">{{unit.title}}</li> </ul> </div>
但是它不起作用。 我知道Promises可以解决,因为我可以将它们logging到控制台,并使用Chromium的开发工具查看这些值。
Promise仍然是asynchronous操作,所以title
方法返回一个promise对象,而不是实际的results
。 您需要使用promise then-able API来提供数据可用时将会调用的callback函数:
app.controller("GreetController", function($scope, SearchService) { SearchService.title('test').then(function(data) { $scope.units = data; }); });
- nw.js /电子最佳embedded式数据库的大型数据集?
- React – variables是未定义的而不是true / false – 组件首先被渲染?
- 如何获得权限来创build一个文件/目录与节点FS
- alasql:保存对象中的所有数据时,应用程序性能不受影响?
- C3.js“未捕获的错误:源数据缺less(1,844)处的组件!”
- 循环select多个select对象的多个选项,并根据SQL查询结果select选项
- 复杂的child_process不能与Promise蓝鸟一起工作
- 可行性检查:图像扫描和PDF生成电子应用程序?
- 'console.log'在Webstorm中不适用于testingElectron应用程序