使用limitTo缩短ng-repeat变量

时间:2017-05-16 10:49:54

标签: javascript angularjs

在我的数据库中,我有像这样的数据结构

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

ng-repeat我去

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

而不是2016我获得了整个{"Year2016":.....}

如果我将查询响应作为字符串放在代码中,它就像怀疑那样工作

由于某种原因,limitTo无法处理新的ng-repeat&#34;变量&#34;

有可能吗?

2 个答案:

答案 0 :(得分:2)

您应该使用(键,值)语法ng-repeat使用循环 json对象数组。< / p>

参考以下示例:

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>

答案 1 :(得分:0)

起始索引的最后一个字段ID,因此你的ng重复应该是

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