AngularJS + ng-repeat : weird $index and $parent. $index 传入函数时的值

标签 angularjs

我有一个 ng-repeat在另一个 ng-repeat .在第二个,当我想通过 $index$parent.$index到函数(使用 ng-clicktooltip ),我没有得到正确的索引。

为了让它工作,我需要使用 $parent.$index$parent.$parent.$index .

奇怪的是,我只需要为第二个 ng-repeat 执行此操作。此外,这些值似乎只有在 ng-click 内时才会出错。或 tooltip (如果我将它们显示在 ng-repeat 中,它们是正确的)

我做了一个plunker,所以你可以看到结果:
http://plnkr.co/edit/K6jWTffe8BqgLZhFIbHL?p=preview

<!DOCTYPE html>
<html>

  <head>
    <link data-require="bootstrap-css@~3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    <script src="https://code.angularjs.org/1.3.0-beta.17/angular.js" data-semver="1.3.0-beta.17" data-require="angular.js@1.3.0-beta.17"></script>
    <script data-require="ui-bootstrap@0.11.0" data-semver="0.11.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script>
      var app = angular.module('myApp', ['ui.bootstrap']);
      app.controller('MainCtrl', function($scope) {
        $scope.data = [
          {id: "1", text: "text 1", more: [
            {id_more: "10", text_more: "text more 10"},
            {id_more: "11", text_more: "text more 11"},
            {id_more: "12", text_more: "text more 12"},
            {id_more: "13", text_more: "text more 13"},
          ]},
          {id: "2", text: "text 2", more: [
            {id_more: "20", text_more: "text more 20"},
            {id_more: "21", text_more: "text more 21"},
            {id_more: "22", text_more: "text more 22"},
            {id_more: "23", text_more: "text more 23"},
          ]},
          {id: "3", text: "text 3", more: [
            {id_more: "30", text_more: "text more 30"},
            {id_more: "31", text_more: "text more 31"},
            {id_more: "32", text_more: "text more 32"},
            {id_more: "33", text_more: "text more 33"},
          ]},
        ];

        $scope.func = function(index){
          var result = index;
          return result;
        };
        $scope.funcMore = function(index, indexMore){
          var result = index + ' - ' + indexMore;
          //The problem is when I want to do $scope.data[index].more[indexMore]
          //Then it throws error because some of the index are undefined in the array
          return result;
        };

        $scope.clickMore = function(index, indexMore){
          alert(index + ' - ' + indexMore);
        };

      });

    </script>
  </head>

  <body ng-app="myApp" ng-controller="MainCtrl" style="margin:20px">
    <div ng-repeat="d in data" style="border:1px solid #ccc; margin-bottom:10px">
      {{d.id}} - {{d.text}} - {{$index}} - 
      <span tooltip-placement="bottom" tooltip-html-unsafe="{{func($index)}}">Working</span>
      <div ng-repeat="more in d.more" style="margin-left:20px">
        {{more.id_more}} - <!-- ID -->
        {{more.text_more}} - <!-- Text --> 
        ({{$parent.$index}} - {{$index}}) <!-- Correct $parent.$index and $index -->
        <span ng-click="clickMore($parent.$parent.$index, $parent.$index)" tooltip-placement="bottom" tooltip-html-unsafe="{{funcMore($parent.$parent.$index, $parent.$index)}}">Working</span> - 
        <span ng-click="clickMore($parent.$index, $index)" tooltip-placement="bottom" tooltip-html-unsafe="{{funcMore($parent.$index, $index)}}">Not Working</span>
      </div>
    </div>
  </body>

</html>

最佳答案

看看documentation对于 ng-init 和 ng-repeat。

使用 ng-init,您可以为 $index 设置别名,这样您就不会遇到所看到的冲突。文档中的示例几乎正是您想要做的。

从文档:

<script>
  angular.module('initExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.list = [['a', 'b'], ['c', 'd']];
    }]);
</script>
<div ng-controller="ExampleController">
  <div ng-repeat="innerList in list" ng-init="outerIndex = $index">
    <div ng-repeat="value in innerList" ng-init="innerIndex = $index">
       <span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span>
    </div>
  </div>
</div>

Here is a modified Plunk

关于AngularJS + ng-repeat : weird $index and $parent. $index 传入函数时的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28682417/

相关文章:

javascript - Angular 数据绑定(bind) - 与 JavaScript 对象的绑定(bind)应不区分大小写

javascript - 将 "then"方法添加到 $promise 链中预定义的 "finally"方法之前

javascript - 显示从 API 调用中以二进制数据形式返回的图像

css - 如果存在滚动条,打开 AngularJS 模式会导致页面移动

javascript - angularjs JSON对象过滤器选择选项

angularjs - 参数 'appCtrl' 不是函数,未定义

javascript - 为什么 Angularjs 检测不到我的 Controller ?

html - Materialisecss卡分布

javascript - 启动和停止 gulp-webserver

javascript - 从嵌套解析函数绑定(bind)到作用域