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

193
Visualizações
AngularJS digest nuance or am I just dumb? ng if renders component twice?

Unfortunately, I am not able to create a reproducible snippet for this scenario. It's occurring in a rather complicated codebase, suffice it to say.

I have a component method onClick that sets a flag to true. e.g.

// in the template
<button type='button' ng-click="onClick()"></button>

//javascript

angular.module('myModule')
  .component('MyComponent', {
  ...
  controller: [function(){
    var ctrl = this;
    ctrl.showModal = false;
    ctrl.onClick = function() {
      ctrl.showModal = true;
    }
  }]
})

This flag is used with an ng-if that instantiates another component that $onInit renders a modal. e.g.

//in the template

<my-modal-component ng-if="$ctrl.showModal"></my-modal-component>


//javascript
angular.module('myModule')
  .component('myModalComponent', {
  ...
  controller: [function(){
    // renders third party modal
    var renderModal = function() {}
   
    // makes and returns an api call to get some data as a promise
    var getRelevantData = function(){}


    this.$onInit = function () {
      getRelevantData().then(function(){
        renderModal();
      })
    }
  }]
})

For some crazy reason, myModalComponent actually gets two separate instances (i.e. renders twice, one on top of the other) when the button is clicked. I know there's some ng-if nuances with scope but I'm not using $scope here, and even putting the flag inside an object didnt work.

ng-show with some if checks on rendering the modal sort of works but my data bindings get all screwed up (a method I pass into myModalComponent is defined when ng-show is false but when ng-show changes to true, it's undefined) so I prefer to not go down that rabbit hole if I can; plus, I think ng-if makes much more sense here.

Even more crazy, if I put a $timeout(, 10000) to delay the setting of the flag to 10 seconds on the button click (onClick), the modal still renders once immediately and ten seconds later, another modal renders on top of it.

Admittedly, I try to stay away from the digest cycle, as I know it's something important to know but ironically is kind of an anti-pattern to mess with... So does anyone have any ideas?

I miss React.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Problem was using angular 1.5.9. Ive seen other digest issues but dealt with them independently but this one was more-or-less a show stopper. So upgraded to 1.8 and problem went away.

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