Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda