Get the first element of an array with AngularJS


I'm starting in and I've already created my first API with .

I have this factory and a controller to consume the data:

  "use strict";

  angular.module('core.controllers', [])
    .controller('CMIUtils', CMIUtils)
    .factory("PolicyFactory", PolicyFactory);

  CMIUtils.$inject = ['$scope', 'PolicyFactory'];
  PolicyFactory.$inject = ['$resource'];

  function CMIUtils($scope, PolicyFactory) {
    $ = new Date();
    $scope.politicas = PolicyFactory.get();

  function PolicyFactory($resource) {
    return $resource(
      { '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"}

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.

asked by toledano 22.04.2016 в 04:13

1 answer


To get the first registration you must do:

<li ng-controller="CMIUtils">{$ politicas.results[0] $}</li>
answered by 22.04.2016 / 04:40