0

I have a list of elements which is plotted via a ng-repeat and I am using a ng-click to get the index of the element of the array I am clicking on.

This is the html code:

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

And this is what is inside the controller:

$scope.periodPercentage = ['a', 'b', 'c', 'd'];
$scope.getIndex = function(period) {
  console.log($scope.periodPercentage.indexOf(period));
};

In another part of the code, I am using again an ng-repeat to show the elements of another array with the same length.

<ul>
  <li ng-repeat="ratio in ratioPercentage">
    {{ratio}}
  </li>
</ul>

Is there a way to show only the {{ratio}} with the same index of {{period}} via ng-show and hide the other ones?

Thanks in advance for your replies!

2
  • where is the code that displays the "other" array? Commented Sep 6, 2016 at 8:35
  • I am going to update my question, but the way is plotted is the same Commented Sep 6, 2016 at 8:36

6 Answers 6

3

It may help you.

<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);
}}
Sign up to request clarification or add additional context in comments.

Comments

1

You could do that:

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

and

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

see updated fiddle

1 Comment

Thanks, but this is not adding anything to my previous code. Maybe could you edit your answer?
1

Something along the lines of:

<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;}

Comments

1

One solution is this:

  $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]];
  }

In the 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>

working code here

Comments

0

Somehing like:

<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])
}

1 Comment

You should use the $index variable!
0

you need to give $index into ng-click

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

1 Comment

Thanks, that's an easier way to get the index, thanks. But how do I ng-show it in the other array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.