Lukas Tilch Lukas Tilch -3 years ago 85
AngularJS Question

shorten ng-repeat variable with limitTo

In my DB I have Datastructure like this

"Projects":[{
"Year2016":[{"Name": "Project1"},{"Name": "Project2"}],
"Year2017":[{"Name": "Project1"},{"Name": "Project2"}]
}]


with
ng-repeat
i go

ng-repeat="year in Projects"
<b>{{year | limitTo:4:6}}</b>


instead of
2016
i get the whole
{"Year2016":.....}


if i put the query response in the code as a string it works as suspected but

for some reason the
limitTo
is not working on the new
ng-repeat
"variable"

Is it possible at all?

Answer Source

you should use a (key, value) syntax ng-repeat to loop through json object array with the keys.

refer the below example:

angular.module("app", [])
  .controller("myCtrl", function($scope) {
    $scope.data = {
      "Projects": [{
        "Year2016": [{
          "Name": "Project1"
        }, {
          "Name": "Project2"
        }],
        "Year2017": [{
          "Name": "Project1"
        }, {
          "Name": "Project2"
        }]
      }]
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <div ng-repeat="project in data.Projects">
    <div ng-repeat="(key, value) in project">
      <b>{{key | limitTo:4:4}}</b>
      <div ng-repeat="item in value">
        <span>{{item.Name}}</span>
      </div>
    </div>
    
  </div>
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download