Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda