【发布时间】:2017-02-26 07:20:35
【问题描述】:
好的,所以我尝试创建一个工厂,它将调用我的 REST api 来检索数据。一切都很好,直到我更新到 Angular 1.6,这迫使我使用 Promises,所以我决定尝试工厂:
var registerApplication = angular.module('applications', ['ajoslin.promise-tracker']);
registerApplication.factory('HTTPFactory', ['$http', function dataService($http) {
var applications = {};
applications = function getApplications() { $http ({
'url': '/adminapi/getApplications',
'method': 'GET',
'cache' : true
}).then(function(response){
// return response.data;
console.log(response);
return response;
});
}
return applications;
}]);
registerApplication.controller("ApplicationController", ['HTTPFactory', function ($scope, HTTPFactory) {
$scope.applications = {};
getApplications();
// when landing on the page, get all applications and show them
function getApplications() {
HTTPFactory.getApplications().then(function(response){
$scope.applications = response;
});
}
}]);
但是我收到了这个错误: angular.js:10859Error: undefined is not an object (evalating 'HTTPFactory.getApplications')
我尝试了几种方法,但似乎都给了我某种形式的错误,我做错了什么???
【问题讨论】:
标签: javascript angularjs rest factory