I'm trying to interact with my RESTful API in my AngularJS app. When I do so, no data displays in my view.
I must be misunderstanding how to use $http + promises. Any ideas what's wrong?
Here is my factory:
angular.module('mycompany.resources').factory('Forms', ['$http', '$q', function($http, $q) {
var Forms = {};
Forms.all = function() {
var deferred = $q.defer();
$http.get('/api/forms.json').then(function(response) {
console.log(response.data);
return response.data;
});
return deferred.promise;
};
return Forms;
}]);
and my controller:
angular.module('mycompany.admin.forms').controller('formListController', ['$scope', 'Forms', function($scope, Forms) {
'use strict';
$scope.forms = Forms.all();
}]);
and my template:
<div ng-controller="formListController">
<ul class="form-list">
<li ng-repeat="form in forms">
<a class="form" href="#/forms/{{form._id}}">
<span class="title">{{form.title}}</span>
<span ng-if="form.lastPublished">Last published {{form.lastPublished | date:'M/d/yy'}}</span>
</a>
</li>
</ul>
</div>
However, if I hardcode data onto the scope, I see data:
angular.module('mycompany.admin.forms').controller('formListController', ['$scope', 'Forms', function($scope, Forms) {
'use strict';
$scope.forms = [
{
"_id": "530f69046c5a65ed1b5a3809",
"archived": false,
"lastPublished": new Date("2014-02-20 14:21:09 UTC"),
"title": "New Student Registration (2014 - 2015)"
}
];
}]);
I understand from this example and this article that I should be able to rely on promises while fetching data in my controller via $scope.forms = Forms.all().