2

I have few div for onclick and few div to show it once onclick,but I want to show only specific div related to particular div.Here my application is working fine,but for 2 different section its repeating,if I click first once fourth div also showing.I have given different function also.Can anyone help me,I am new to angularjs. Below is the code.

HTML

<script  src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<script src="app.js"></script>

<body ng-app="plunker" ng-controller="MainCtrl">
  <div ng-repeat="item in items">
    <div style="color:red" ng-click="onClick1($index,0)">first</div>
    <div style="color:blue" ng-show="div_[$index+'_0']">yyy</div>
    <br />
    <hr />
  </div>
  <div ng-repeat="item in items2">
    <div style="color:red" ng-click="test($index,0)">second</div>
    <p style="color:blue" ng-show="div_[$index+'_0']">zzz</p>
    <br />
    <hr />
  </div>
</body>

app.js

  angular.module('plunker', [])
    .controller('MainCtrl', function($scope) {
      $scope.div_=[];
      $scope.items = [
        {
        // id: 1,
          title: "first item"
        },
        {
        // id: 2,
          title: "second item",
        },
        {
        // id: 3,
          title: "third item",
        }
      ];
      $scope.items2 = [
        {
          id: 5,
          title: "first item 1"
        },
        {
          id: 6,
          title: "second item 2",
        },
        {
          id: 7,
          title: "third item 3",
        }
      ];
      $scope.onClick1 = function (indexa, row1){
        alert('first');
        $scope.div_[indexa+'_'+row1]=true;
      }
      $scope.test = function (indexb, row2){
        $scope.div_[indexb+'_'+row2]=true;
      }
    });
3
  • 1
    Can you please rephrase the question? I don't understand it.. Commented Jun 19, 2018 at 18:02
  • here my requirement is to show specific related div When I click a div.But here the issue is when I click first div of first section,first div of second section is also showing.just run the above code and click div one by one you can able to understand Commented Jun 19, 2018 at 18:17
  • I'm not able to see in this plunker what you're saying Commented Jun 19, 2018 at 18:23

1 Answer 1

1

You are using the same variable to show/hide the div's, you've already built something to work around this, but is not using. Try this:

<div style="color:red" ng-click="test($index, 1)">second</div>
<p style="color:blue" ng-show="div_[$index+'_1']">zzz</p>

The div_[$index+'_0'] is now div_[$index+'_1'] And test($index, 0)must be test($index, 1)

Sign up to request clarification or add additional context in comments.

3 Comments

I have modified but if I click now on 'second' I mean fourth div,nothing is showing,it should show 'zzz'
the test($index, 0) must be test($index, 1)
Hey, could you mark the answer as correct? It helps a lot! :)

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.