Angular $ index ng-repeat CLOSED

时间:2015-06-21 13:32:25

标签: javascript html angularjs

我有一个阵列:[petra,petrita,martin,jose] 在我的ng-repeat我展示了类似的内容:

Petra with Img 0
Martin with Img 1
Jose with Img 2
Petra with Img 3
Jose with Img 4
Petrita wih Img 5

但是,当我使用带$ index的函数时 该函数返回数组中的位置,例如:

when i click petra with img 0, return petra
when i click martin with img1, return petrita
when i click jose with img2,  return martin
when i click petra with img3, return jose
when i click jose with img4, return undefined
etc etc

当我点击某人时我想要这个名字,当我点击马丁与img 1我需要功能返回马丁而不是petrita。 有什么问题?

代码:

    $scope.thisUser = function(index){
          share.setUseract($scope.users[index]);
          console.log(share.getUseract());  
        }
      }
     //in my custom service i have something like:
     setUseract: function(data){
       usuario = data;
     },
     getUseract: function(){
       return usuario;
     }

HTML:

 <div class="col-md-offset-2 col-md-8 margentop" ng-repeat="user in users"><span class="nombreimg"><a href="" ng-click="thisUser($index)">{{user}}</a></span></div>

1 个答案:

答案 0 :(得分:0)

一旦获得索引,您可以尝试使用swirch案例来获取图像:     var name;     switch($ index){         案例0:         案例3:             名字=&#39;佩特拉&#39;
            打破;         案例2:         案例4:             姓名=&#39;何塞&#39;             打破;         ...     }

return name