angularjs - isolate scope between multiple different directives

Ciel

I am trying to create three different directives that need to communicate together, and this isn't really a problem but I would like to explore the possibility of using an isolate scope, or at least as small a scope as possible between them.

Because sharing directly on the controller scope is so discouraged, I enacted this to try and get around that. The only solution I've seen immediately is to make all three behaviors in the same single directive, so that they all use the same scope - but this is very messy.

This is an extremely slim example of how it works. The full code is very long and it's not useful without a lot of extra context. I'm trying to trim it down to just what I need help with.

1. Is there a better way to share these scope properties between the directives?

2. Can I do it using an isolate scope, to reduce the clutter and load on the controller?

3. The reason for sharing information is a bit hard to explain briefly.

directives

angular.module('app', [])
.directive('dataSourceUrl', ['$parse', function($parse) {
   return {
        restrict: 'A',
        priority: 10,
        link: function(scope, element, attributes) {
            var path = attributes.dataSourceUrl;
                
            angular.extend(scope.options, {
                /* more code */
            });
        }
   }
}])
.directive('dataSourceOptions', 
    ['$parse', function($parse) {
    return {
        restrict: 'A',
        priority: 9,
        link: function(scope, element, attributes) {
            var options = scope.$eval(attributes['dataSourceOptions']);
            
            angular.extend(scope.options, {
                /* more code */
            });
        }
    }
}])
.directive('dataSourceColumns', 
    ['$parse', function($parse) {
    return {
        restrict: 'A',
        priority: 9,
        link: function(scope, element, attributes) {
            var columns = scope.$eval(attributes.dataSourceColumns);
            
            angular.forEach(columns, function(value, key) {
                switch(value) {
                    case "Id":
                        scope.options.columns.push({ field: "Id" /* more code */ });
                        break;
                    case "Name":
                        scope.options.columns.push({ field: "Name" /* more code */ });
                        break;
                    /* more code */
                }
            });
        }
    }
}]);

notes

My experience with angular is minimal, I am very new. I have researched isolate scope extensively, but it is proving to be more difficult than I thought. My goal is to use these directives kind of like this ...

<div kendo-grid data-source-url="'path/to/data'" 
     data-source-options="options" 
     data-source-columns="['Id','Name','Abbreviation','Group']">
</div>
harishr

you can have multiple isolate scoped directive on same element. though as far as i remember, read it somewhere, that multiple isolate scoped directive do share the scopes.

you can read more about scopes here

regarding sharing the data between directives, are the directives independent or are they in parent-child relationship?

if parent child then use require property of directives. and pass data only to parent directive and access it in child directive.

if they are sibling or independent, pass data to each directive separately.

lets assume that dataSourceUrl display changes on basis of parameters in dataSourceOptions. then you can use require property.

.directive('dataSourceUrl', 
    ['$parse', function($parse) {
    return {
        restrict: 'A',
        require: 'dataSourceOptions'
        link: function(scope, iElement, iAttrs, ctrlOptions)
               {
                         //ctrlOption.showHeadeers or something
                }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angularjs - isolate scope between multiple different directives

From Dev

Isolate scope in Angularjs custom directives - "="

From Dev

AngularJS : Scope is not shared between nested transcluded directives

From Dev

AngularJs: multiple directives asking for isolated scope on

From Dev

angularjs - Multiple directives asking for new / isolated scope

From Dev

AngularJS isolate scope: & vs =

From Dev

Isolate scope fallback in AngularJS

From Dev

AngularJs multiple controllers of the same name in one file isolate scope

From Dev

AngularJs multiple controllers of the same name in one file isolate scope

From Dev

AngularJS : directives and scope

From Dev

AngularJs scope reference with directives

From Dev

problems with the scope variable over communication between the directives in angularjs

From Dev

AngularJS Documentation on Isolate Scope Attributes

From Dev

AngularJS Directive Isolate scope behavior

From Dev

AngularJS Documentation on Isolate Scope Attributes

From Dev

How can I bind ngModel through multiple directives with isolate scopes in AngularJs?

From Dev

Confuse about the AngularJS scope when multiple directives share same element

From Dev

angularjs inheriting scope in nested directives

From Dev

AngularJs attributes vs scope in directives

From Dev

AngularJS : Nested Directives and Scope Inheritance

From Dev

AngularJs attributes vs scope in directives

From Dev

AngularJS communication between directives

From Dev

AngularJS: Communication between directives

From Dev

multiple custom directives angularjs

From Dev

AngularJS directives - Isolated Scope and Inherited Scope

From Dev

Multiple custom directives scope in Angular

From Dev

Isolate scope directive with optional ampersand binding in angularjs?

From Dev

AngularJS : Directive not able to access isolate scope objects

From Dev

AngularJS Jasmine Test - Element Isolate Scope undefined

Related Related

  1. 1

    angularjs - isolate scope between multiple different directives

  2. 2

    Isolate scope in Angularjs custom directives - "="

  3. 3

    AngularJS : Scope is not shared between nested transcluded directives

  4. 4

    AngularJs: multiple directives asking for isolated scope on

  5. 5

    angularjs - Multiple directives asking for new / isolated scope

  6. 6

    AngularJS isolate scope: & vs =

  7. 7

    Isolate scope fallback in AngularJS

  8. 8

    AngularJs multiple controllers of the same name in one file isolate scope

  9. 9

    AngularJs multiple controllers of the same name in one file isolate scope

  10. 10

    AngularJS : directives and scope

  11. 11

    AngularJs scope reference with directives

  12. 12

    problems with the scope variable over communication between the directives in angularjs

  13. 13

    AngularJS Documentation on Isolate Scope Attributes

  14. 14

    AngularJS Directive Isolate scope behavior

  15. 15

    AngularJS Documentation on Isolate Scope Attributes

  16. 16

    How can I bind ngModel through multiple directives with isolate scopes in AngularJs?

  17. 17

    Confuse about the AngularJS scope when multiple directives share same element

  18. 18

    angularjs inheriting scope in nested directives

  19. 19

    AngularJs attributes vs scope in directives

  20. 20

    AngularJS : Nested Directives and Scope Inheritance

  21. 21

    AngularJs attributes vs scope in directives

  22. 22

    AngularJS communication between directives

  23. 23

    AngularJS: Communication between directives

  24. 24

    multiple custom directives angularjs

  25. 25

    AngularJS directives - Isolated Scope and Inherited Scope

  26. 26

    Multiple custom directives scope in Angular

  27. 27

    Isolate scope directive with optional ampersand binding in angularjs?

  28. 28

    AngularJS : Directive not able to access isolate scope objects

  29. 29

    AngularJS Jasmine Test - Element Isolate Scope undefined

HotTag

Archive