Nested Angular directives triggering scope functions on parents

jonhobbs

I have created a fairly simple accordian which comprises an accordian directive and accordian-item directives. The outer accordian directive simply provides a way for the items to register themselves and communicate (e.g. when one is clicked the others should close).

The accordian seems to work correctly until I nest one inside another. When I open or close a panel belonging to the inner accordian it toggles the containing item of the parent accordian.

I know it is something to do with inherited scopes because if I console.log(scope) from the inner accordian it logs 2 scope objects, but I'm not sure how to get te inner accordian to not inherit the parent's scope and still work correctly as it needs to have access to the HTML attributes I've given it access to.

Hope fully the code will make more sense.

angular.module('app.directives').directive('AccordianItem', [function () {

    return {

        require:'^Accordian',
        restrict: 'EA',
        scope: {
            isOpen: '=?',
            isDisabled: '=?'
        },

        link: function (scope, element, attrs, accordionCtrl) {


            // Watch the isOpen variable
            scope.$watch('isOpen', function(value) {

                // Open or close this panel
                if (value){
                    scope.openPanel();
                }
                else{
                    scope.closePanel();
                }

            });


            scope.openPanel = function(){

                // Removed for brevity

            };

            scope.closePanel = function(){

                // Removed for brevity

            };



            // Toggle function
            scope.toggleOpen = function() {

                // Removed for brevity

            };


            // Add trigger behaviour

            element.find('.accordian-trigger').on('click', function (event) {

                scope.toggleOpen();


            });


        }        

    };

}]);

Any advice would be gratefully received.

jonhobbs

Doh! I was being pretty stupid. The inner directive was not calling the outer directive's toggle function, the outer directive was using the inner directive's trigger for itself. Here is the offending line.

element.find('.accordian-trigger').on('click', function (event) {

It will obviously attach the behaviour to any elements with that class in any sub directives too.

Now if only I could find a jQuery selector which would look for ".accordian-trigger" but not go any deeper when it gets to an ".accordian"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Binding functions through nested directives with isolated scope failing in Angular 1.4

From Dev

Scope Isolation & nested directives

From Dev

Scope inheritance for angular directives

From Dev

angular scope in controllers + directives

From Dev

Scope inheritance for angular directives

From Dev

angularjs inheriting scope in nested directives

From Dev

AngularJS : Nested Directives and Scope Inheritance

From Dev

Nested directives break angular

From Dev

Angular Directives: scope vs bindToController

From Dev

Multiple custom directives scope in Angular

From Dev

In nested Angular directives, can the child directive inherit a variable from parent's isolated scope?

From Dev

Passing params to nested directives with isolated scope

From Dev

How does one preserve scope with nested directives?

From Dev

How does one preserve scope with nested directives?

From Dev

AngularJS : Scope is not shared between nested transcluded directives

From Dev

Strange behavior of nested Angular directives

From Dev

Nested Angular directives not working as expected

From Dev

How to stop propagation of & functions into nested directives

From Dev

Angular How to access controller scope from directives

From Dev

Angular:How to pass scope data to directives

From Dev

R scope rules for nested functions

From Dev

Python: Nested functions and variable scope

From Dev

Variable scope for nested functions in python

From Dev

Scope in directives

From Dev

Scope in directives

From Dev

Trouble with Angular Nested Directives when using ControllerAs

From Dev

Wraping angular directives,controllers and routes into functions

From Dev

Wraping angular directives,controllers and routes into functions

From Dev

Calling a parent scope function from deeply nested directives

Related Related

  1. 1

    Binding functions through nested directives with isolated scope failing in Angular 1.4

  2. 2

    Scope Isolation & nested directives

  3. 3

    Scope inheritance for angular directives

  4. 4

    angular scope in controllers + directives

  5. 5

    Scope inheritance for angular directives

  6. 6

    angularjs inheriting scope in nested directives

  7. 7

    AngularJS : Nested Directives and Scope Inheritance

  8. 8

    Nested directives break angular

  9. 9

    Angular Directives: scope vs bindToController

  10. 10

    Multiple custom directives scope in Angular

  11. 11

    In nested Angular directives, can the child directive inherit a variable from parent's isolated scope?

  12. 12

    Passing params to nested directives with isolated scope

  13. 13

    How does one preserve scope with nested directives?

  14. 14

    How does one preserve scope with nested directives?

  15. 15

    AngularJS : Scope is not shared between nested transcluded directives

  16. 16

    Strange behavior of nested Angular directives

  17. 17

    Nested Angular directives not working as expected

  18. 18

    How to stop propagation of & functions into nested directives

  19. 19

    Angular How to access controller scope from directives

  20. 20

    Angular:How to pass scope data to directives

  21. 21

    R scope rules for nested functions

  22. 22

    Python: Nested functions and variable scope

  23. 23

    Variable scope for nested functions in python

  24. 24

    Scope in directives

  25. 25

    Scope in directives

  26. 26

    Trouble with Angular Nested Directives when using ControllerAs

  27. 27

    Wraping angular directives,controllers and routes into functions

  28. 28

    Wraping angular directives,controllers and routes into functions

  29. 29

    Calling a parent scope function from deeply nested directives

HotTag

Archive