Thursday, 5 September 2013

Get information from an api with Angularjs $resource

Get information from an api with Angularjs $resource

I have access to a JSON api, the api URL is http://host/api, when I ask
the api with the following parameters
http://host/api&q=count_people&on=house&house_id=123433
the api response is:
{
people: "6"
}
the api works like the previous example, now when I trying to use in my
controller i have the following code
//app.js
var app = app.module('myapp', ['ngResource']);
app.factory('API', function($resource) {
var API = $resource('host/api');
return {
get: function(o, q, params) {
s = API.get({on: o, q: q, house_id: params.id}, function(u) {
return u;
})
return s;
}
};
});
app.controller('myController', function($scope, API) {
$scope.ns = {
count_people: function() {
return API.get('house', 'count_people', {id: 123433});
}
};
});
in my template i have
<!-- mytemplate.html -->
<p>Total of people in house: {{ns.count_people().people}}</p>
but the ns.count_people().people never is called

No comments:

Post a Comment