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

121
Vistas
AngularJs custom directive used twice in one controller with $watch

Hi I have a controller which has a param called refresh and a $watch

app.controller (..., []) {
  $scope.refresh = false;

  $scope.$watch('refresh', function() {
            if(!$scope.refresh) return;
             $scope.apiCall(true);
            
   }, true);
}

I have a directive where refresh is used.

app.directive('dirA', () => {
  return {
    restricts: E,
    scope: {
      refresh: '=',
      .....
    },
    templateUrl: 'location of template',
    controller: [..., {
        function test() {
          $scope.refresh = false;
          // API call here
          fetch('/test', () => {
            this.refresh = true;
          )}
        }
    }]
  }
})

In my Controller's template I am using this directive twice like

<dirA refresh="refresh"/>
<dirA refresh="refresh"/>

Issue that I am facing is when refresh changes from first instance of directive, $watch is called but same is not happening from second instance.

I am pretty new to angularjs, Please help if possible

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When you declare a controller in a directive, you also need to bind the isolate scope of your directive using bindToController.

Here is an article that explains bindToController

I also took the time to write a plunkr that demonstrates with the code you provided how this should probably look.

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