0

I want to show the count of items repeated in my ng-repeat but how to I access this from outside the ng-repeat?

Plunker: http://plnkr.co/edit/SYqb8h9k81SlVY3Yzhgx

HTML:

<h3>Number of locations is: {{location.length}}</h3>

<div ng-controller="locationAccordionCtrl">
  <div ng-repeat="location in locations" on-finish-render>

    {{location.siteName}}

    <ul>
        <li ng-repeat="listOfLocations in location.locationsList track by $index">
            {{listOfLocations}}
        </li>
    </ul>

  </div>
</div>

JS:

var App = angular.module('App', []);

App.controller('locationAccordionCtrl', function ($scope) {

  $scope.locations = [
    {
      "siteName":"First Location",
      "locationsList":['First Location 1', 'First Location 2', 'First Location 3']
    },
    {
      "siteName":"Second Location",
      "locationsList":['Second Location 1', 'Second Location 2', 'Second Location 3']
    },
    {
      "siteName":"Third Location",
      "locationsList":['Third Location 1', 'Third Location 2', 'Third Location 3']
    }
  ];

});

4 Answers 4

1

The <h3> tag must be in the scope of the locationAccordionCtrl. So, put it inside the <div ng-controller="locationAccordionCtrl"> tag. Also, you have to say {{locations.length}} instead of {{location.length}} (missing the "s")

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is not that it is outside of ng-repeat, but rather that it is outside your controller. The following should work:

<div ng-controller="locationAccordionCtrl">
<h3>Number of locations is: {{locations.length}}</h3>
[...]

Comments

0

Put the bind inside the controller:

<div ng-controller="locationAccordionCtrl">
  <h3>Number of locations is: {{locations.length}}</h3>
  <div ng-repeat="location in locations" on-finish-render>

    {{location.siteName}}

    <ul>
        <li ng-repeat="listOfLocations in location.locationsList track by $index">
            {{listOfLocations}}
        </li>
    </ul>

  </div>
</div>

Comments

0
<div ng-controller="locationAccordionCtrl">

<h3>Number of locations is: {{locations.length}}</h3>

<div ng-repeat="location in locations" on-finish-render>

{{location.siteName}} ({{location.locationsList.length}})

<ul>
    <li ng-repeat="listOfLocations in location.locationsList track by $index">
        {{listOfLocations}}
    </li>
</ul>

</div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.