我正在尝试创建一个服务,该服务首先通过使用 $http 进行 AJAX 调用来加载一些数据。
我正在看类似的东西:
app.factory('entityFactory', function() {
var service = {};
var entities = {};
// Load the entities using $http
service.getEntityById(entityId)
{
return entities[entityId];
}
return service;
});
app.controller('EntityController', ['$scope', '$routeParams', 'entityFactory', function($scope, $routeParams, entityFactory) {
$scope.entity = entityFactory.getEntityById($routeParams['entityId']);
}]);
我想确保在使用返回实体之前完全加载实体getEntityById
。
请让我知道这样做的正确方法是什么?我知道的一种方法是进行同步 AJAX 调用,但有更好的方法吗?在这种情况下可以以更好的方式使用 Promise 吗?