1

I have two directives: directiveA and directiveB, hanging from the same module in my AngularJS application. They are called at the same level in the same HTML template, so we could say they are brothers.

<directive-a>
<directive-b>

Both of them have an own method hanging from its scope, like this:

$scope.clickOkey = function () {
       ... whatever
     };

They both have a 'clickOkey' method, but their behaviours are different. My problem comes out when I try to call the 'clickOkey' of directiveA from directiveA's template. It executes the 'clickOkey' from directiveB.

Inside directiveA's own template:

<label ng-click="clickOkey()">Okey</label>

They are placed at same level so there it shouldn't be way for them to share their $scope or misunderstanding methods.

Also, is important to say that if I change the method's name to 'clickOkeyA', for example, it takes the right method, so the template can access to its scope without problems.

What am I missing? Thanks for your help!

Edit: Both directives are isolated and have a controller, and inside each one of them is defined a 'clickOkey' method. There are two methods with the same name.

Both directives are like this:

angular.module('myModule').directive('directiveA', function () {
    return {
        restrict: 'AE',
        templateUrl: '/whatever.html',
        controller: function ($scope, $http, $rootScope) {

            $scope.clickOkey = function () {
               ... whatever
             };
        }
    }
});
5
  • you should isolate scope of both directive. by default, directive share parent scope. Commented Aug 21, 2015 at 9:12
  • Define controller in both directives so that method will be in their own isolated scope and will work properly. Commented Aug 21, 2015 at 9:16
  • Yes, they are isolated and both directives have controller defined. Inside both controllers is where the two 'clickOkey' methods are defined. Commented Aug 21, 2015 at 9:18
  • can you show you directive codes pls Commented Aug 21, 2015 at 9:19
  • You've got no isolated scope, I'll edit my answer Commented Aug 21, 2015 at 9:27

1 Answer 1

1

You should add an isolated scope to your directives : https://docs.angularjs.org/guide/directive

For your directive it would be :

angular.module('myModule').directive('directiveA', function () {
  return {
    restrict: 'AE',
    templateUrl: '/whatever.html',
    scope : {},
    controller: function ($scope, $http, $rootScope) {

        $scope.clickOkey = function () {
           ... whatever
         };
    }
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

That's exactly how it is, an isolated controller for each directive.
In what you'v send it's missing the scope : {}, line
Okey, I missed that line of: "scope : {}," You're right, that was the problem. Now it's working. Thank you very much. Still I don't understand why "brother" directives share the same scope.
You'r welcome ! They both share the parent scope in fact

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.