【问题标题】:How to write two ng-repeat by mixing the values?如何通过混合值来编写两个 ng-repeat?
【发布时间】:2016-06-30 05:50:42
【问题描述】:

我有 2 个 json ,

$scope.start_date=[{'01/02/2016'},{'05/05/2014'},{'08/02/2015'},{'04/08/2016'}]
$scope.end_date=[{'01/02/2016'},{'05/05/2014'},{'08/02/2015'},{'04/08/2016'}]

我必须使用 ng-repeat 显示第一个 start_date 和第一个 end_date,第二个 start_date 和第二个 end_date。是否可以使用 ng-repeat 使用 anguarjs 显示这样的显示。谁能帮帮我

【问题讨论】:

  • 我认为那些不是有效的对象
  • 所以我需要将 start_date 和 end_date 放在一个对象中才能使其工作?
  • $scope.start_date=[{'01/02/2016'},{'05/05/2014'},{'08/02/2015'},{'04/08/ 2016'}] 就这样传下去
  • 我建议你用一个循环合并你的 2 个数组,然后在你的输出数组上进行一次 ng-repeat。
  • @01axel01christian ,我明白了你的建议,但我怀疑是否有使用 ng-repeat 显示的内置方式

标签: javascript angularjs json ng-repeat


【解决方案1】:

认为你正在寻找类似下面的东西。有几种方法可以做到这一点。 ng-repeat 公开 $index,您可以使用它来显示另一个数组中的项目,假设两个数组具有相同数量的项目。

<ul>
  <li ng-repeat="date in start_date">Start: {{date}}, End: {{end_date[$index]}}</li>
</ul>

你应该制作这些数组:

$scope.start_date=['01/02/2016','05/05/2014','08/02/2015','04/08/2016'];
$scope.end_date=['01/02/2016','05/05/2014','08/02/2015','04/08/2016'];

【讨论】:

  • 假设每个 start_date 都有一个特别对应的 end_date,最好将startend 放在一个对象中,然后将它们放在一个数组中(如@kiro112 的答案) .
【解决方案2】:

你可以像这样格式化你的日期

dates = [{
    start: "01/01/2016",
    end: "01/30/2016"  
}, {
    start: "02/01/2016",
    end: "02/28/2016"  
}];

然后你可以在你的ng-repeat 中使用它们

<div ng-repeat="date in dates">
    <span>{{date.start}} - {{date.end}}</span></br>
</div>

【讨论】:

    【解决方案3】:
    $scope.dates=[{start_date :'01/02/2016',end_date :'01/02/2016''},{start_date :'05/05/2014', end_date:'05/05/2014'}...]
    

    像这样传递日期数组并使用 ng-repeat as

     <ul><li ng-repeat="date in dates">start date : {{date.start_date}} End date : {{date.end_date}}</li></ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多