I'm starting in angularjs and I've already created my first API with djangorestframework .
I have this factory and a controller to consume the data:
(function(){
"use strict";
angular.module('core.controllers', [])
.controller('CMIUtils', CMIUtils)
.factory("PolicyFactory", PolicyFactory);
CMIUtils.$inject = ['$scope', 'PolicyFactory'];
PolicyFactory.$inject = ['$resource'];
function CMIUtils($scope, PolicyFactory) {
$scope.date = new Date();
$scope.politicas = PolicyFactory.get();
}
function PolicyFactory($resource) {
return $resource(
"/api/v1.0/politica/",
{},
{ 'get': {method: "GET", isArray: false}}
);
}
})();
On my page, I use <li ng-controller="CMIUtils">{$ politicas.results $}</li>
and what I get, of course this is:
<li ng-controller="CMIUtils" class="ng-scope ng-binding">
[
{"revision":4,"fecha":"2016-04-16","politica":"Esta es la cuarta política"},
{"revision":3,"fecha":"2016-04-04","politica":"Esta es la tercera política"},
{"revision":2,"fecha":"2016-04-02","politica":"Esta es la segunda política"},
{"revision":1,"fecha":"2016-04-01","politica":"Esta es la primera política"}
]
</li>
The list, as such, will be used at another time with ng-repeat
, but at this time, I would like to know how to get the first element of this list .
I'm not saying how to get the detail , only the first element of any list generated with the
.factory()
method.