单击一个数组元素,显示与另一个数组具有相同索引的元素

时间:2016-09-06 08:31:28

标签: javascript html angularjs arrays

我有一个通过c:\\folder\\resource.txt绘制的元素列表,我使用ng-repeat来获取我点击的数组元素的索引。

这是html代码:

ng-click

这就是控制器内部的内容:

<ul>
  <li ng-repeat="period in periodPercentage" ng-click="getIndex('{{period}}')">
    {{period}}
  </li>
</ul>

在代码的另一部分中,我再次使用$scope.periodPercentage = ['a', 'b', 'c', 'd']; $scope.getIndex = function(period) { console.log($scope.periodPercentage.indexOf(period)); }; 来显示具有相同长度的另一个ng-repeat的元素。

array

是否可以通过<ul> <li ng-repeat="ratio in ratioPercentage"> {{ratio}} </li> </ul> 仅显示{{ratio}}的{​​{1}}索引,并隐藏其他索引?

提前感谢您的回复!

6 个答案:

答案 0 :(得分:3)

它可能对你有帮助。

<div ng-app ng-controller="testrahul">
<ul>
  <li ng-repeat="(i,period) in periodPercentage" ng-click="getindex(i)">
    {{period}}
  </li>
</ul>

function testrahul($scope) {
$scope.periodPercentage = ['a', 'b', 'c', 'd'];
$scope.getindex = function(i){
    console.log(i);
}}

答案 1 :(得分:1)

你可以这样做:

<li ng-repeat="period in periodPercentage" ng-click="getindex($index)">
    {{period}}
</li>

<ul>
  <li ng-repeat="ratio in ratioPercentage" ng-show="$index == findex">
    {{ratio}}
  </li>
</ul>

see updated fiddle

答案 2 :(得分:1)

有些事情:

<ul>
  <li ng-repeat="period in periodPercentage" ng-click="setvariable($index)">
    {{period}}
  </li>
</ul>

<ul>
  <li ng-repeat="ratio in ratioPercentage" ng-show="someVariable == $index">
    {{ratio}}
  </li>
</ul>


function setVariable(index){ $scope.someVariable = index;}

答案 3 :(得分:1)

一个解决方案就是:

  $scope.periodPercentage = ['a', 'b', 'c', 'd'];
  $scope.ratePercentage = ['arate', 'brate', 'crate', 'drate'];
  $scope.rateShowPercentage = angular.copy($scope.ratePercentage);
  $scope.update = function(index){
    $scope.rateShowPercentage  = [$scope.ratePercentage[index]];
  }

在HTML

<div ng-controller="GreetingController">
<ul >
  <li ng-repeat="period in periodPercentage" ng-click="update($index)">
    {{period}}
  </li>
</ul>

<ul >
  <li ng-repeat="rate in rateShowPercentage" >
    {{rate}}
  </li>
</ul>
</div>

工作代码here

答案 4 :(得分:0)

Somehing如:

<ul>
  <li ng-repeat="object in list" ng-click="openOther($index)">
    {{object.data}}
  </li>
</ul>

<ul>
  <li ng-repeat="object in otherListToShow">
    {{object.data}}
  </li>
</ul>

and:

var otherList = [ some data ...]
 var otherListToShow = []

openOther(index){
listToShow.push(otherList[index])
}

答案 5 :(得分:0)

您需要将$ index纳入ng-click

 <ul>
 <li ng-repeat="period in periodPercentage" ng-click="getIndex('{{$index}}')">
  {{period}}
</li>