【问题标题】:Add div using ng-if with ng-repeat使用 ng-if 和 ng-repeat 添加 div
【发布时间】:2016-07-05 22:40:29
【问题描述】:

当 $index == 2 时,我正在尝试使用 ng-repeat 呈现 div。 在这种情况下,将显示“法国”索引 2。任何帮助将不胜感激

$scope.Countries = [
{ Name : 'USA'},
{ Name : 'Japan'},
{ Name : 'France'},
{ Name : 'Canada'},
{ Name : 'China'},
];

<div ng-repeat="country in Countries>
<div class="col-md-2" ng-if="$index == 2">
<a href="">{{country.Name}}</a>
</div>

【问题讨论】:

  • 你只是漏掉了div标签中的引号。

标签: javascript angularjs angularjs-ng-repeat angular-ng-if


【解决方案1】:

看起来你没有关闭 ng-repeat div。缺少$track by index 也可能是个问题。

<div ng-repeat="country in Countries track by $index">
  <div class="col-md-2" ng-if="$index == 2">
    <a href="">{{ country.Name }}</a>
  </div>
</div>

【讨论】:

【解决方案2】:

您为什么要这样做?为什么不只显示索引 2?

 <div class="col-md-2">
     <a href="">{{ Countries[2].Name }}</a>
 </div>

【讨论】:

  • 我希望索引为3或其他数字时发生一些事情,学习porpuses
  • @GilbertoQuintero 在控制器中设置activeItem 或类似属性可能比通过索引切换更实用
【解决方案3】:

你可以试试这样的:

<div ng-repeat="country in countries">
     <div class="col-md-2" ng-if="$index == 2">
         <a href="">{{country.Name}}</a>
     </div>
</div>

【讨论】:

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