【发布时间】:2016-12-19 22:02:02
【问题描述】:
我正在使用组件结构构建一个 Angular 1.5 应用程序。在服务中的 $http 调用返回承诺后,我试图调用另一个函数来过滤数据集,然后再将其显示在 UI 上。
但是,filterApps 函数没有被调用。
另外...在 filterApps 函数中,我试图与对象数组进行比较并返回具有相同名称的对象。这是解决此问题的最佳方法还是有更清洁的方法?
控制器:
import allApps from '../../resources/data/application_data.js';
class HomeController {
/*@ngInject*/
constructor(ItemsService) {
this.itemsService = ItemsService;
this.displayApps = [];
}
$onInit() {
this.itemsService
.getItems()
.success((apps) => this.filterApps(apps));
}
filterApps(siteApps) {
this.displayApps = allApps.applications.filter((app) => {
siteApps.applications.map((siteApp) => {
if(siteApp.name === app.name) {
return app;
}
})
});
}
}
export default HomeController;
【问题讨论】:
-
我没有看到任何
$http电话... -
真的进入成功回调吗,试试加
.error((error) => console.log(error))?你确定getItems确实返回了一个承诺.. 检查控制台以及错误 -
@DannyBuonocore $http 调用在与问题无关的服务中
-
@PankajParkar 是的,它确实进入了成功回调。以前我有
this.itemsService .getItems() .success((apps) => this.displayApps = apps);并显示所有应用程序 -
您会惊讶于这里有多少问题无法回答,因为 OP 认为违规代码“与问题无关”......只是说'
标签: javascript angularjs