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

122
Views
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 answers
Answer question

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 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!