Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
AngularJS: How do you change a button and other elements within an ng-repeat with a click of said button?

Let's say that I have some code:

<div ng-app="myApp" ng-controller="myCtrl">
  <div class="hey">
      <div class="dude" ng-repeat="thing in things">
          <button type="button" ng-click="doStuff()">click me</button>
          <div ng-repeat="p in {{thing.parts}}">Part {{$index}}: {{p}}</div>
      </div>
  </div>
</div>

Now, let's say that I want to make it so that each "thing" in the "things" array has its own corresponding button. When I click on the button that corresponds to one particular "thing", the entries in the "parts" array (a property of a "thing") will be displayed and the text "click me" will change to "been clicked". When you click the button again, the entries of the parts array for that "thing" will not be displayed, and the text for the button will go back to saying "click me".

How exactly would I do this? I'm not quite sure how exactly to manipulate individual elements within an ng-repeat.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need somewhere to store if a "thing" is active or not. You could either add an isActive property on thing or add an activeThing property on your VM.

For this example, put it on your VM.

Then put a div with an ng-if inside your outer ng-repeat, like so:

<div ng-app="myApp" ng-controller="myCtrl">
  <div class="hey">
      <div class="dude" ng-repeat="thing in things">
          <div ng-if="thing !== activeThing">
            <button type="button" ng-click="activeThing = thing">click me</button>
          </div>
          <div ng-if="thing === activeThing">
            <button type="button" ng-click="activeThing = undefined">Clicked</button>
            <div ng-repeat="p in {{thing.parts}}">Part {{$index}}: {{p}}</div>
          </div>
          
      </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can track by id or track by index.

angular.module('myApp', [])
  .controller('myCtrl', ['$scope', function($scope) {
    $scope.things = [{
        id: 1,
        title: "Thing Foo",
        parts: ["a", "b", "c"]
      },
      {
        id: 2,
        title: "Thing Bar",
        parts: ["d", "e", "f"]
      }
    ];

    $scope.doStuff = function(id, index) {
    let thing
    if (id)  thing = $scope.things.find(t => t.id === id)
    else  thing = $scope.things[+index]
      console.log(`${thing.title} clicked`);
      thing.show = !thing.show;
      if (!thing.show) return;
      let c = thing.numClicks ? +thing.numClicks + 1 : 1;
      thing.numClicks = c;
    }
  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <div class="hey">
    <div class="dude" ng-repeat="thing in things">
      <button type="button" ng-click="doStuff(thing.id)">click me (id)</button>
      <button type="button" ng-click="doStuff(false, $index)">click me (index)</button>
      <div ng-show="thing.show">
        <h3>Clicked {{thing.numClicks}} times</h3>
        <div ng-repeat="p in thing.parts">Part {{$index}}: {{p}}</div>
      </div>
      <hr>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!