9

I'm new to angularjs and I don't know if this is possible and if possible how to implement it. I want to create a custom directive with a controller that uses information passed to it through attributes. This is a non working example of what I want to implement:

The HTML should look like:

<div ng-app="test">
    <custom-directive attr1="value1" attr2="value2"></custom-directive>
</div>

And the js:

   var app = angular.module('test', [ ]);
    app.directive("customDirective", function(){
        return {
            restrict: 'E',
            scope: ???,
            controller: function(){
                console.log("print attributes value: " + attr1 + ", " +  attr2 );
            }
        }
    };
});

And the expect console output should be:

print attributes value: value1, value2

Any idea? Thanks!

3 Answers 3

7

In your directive you can define the scope objects (attributes) you want to access and use as follows:

app.directive("customDirective", function(){
    return {
        restrict: 'E',
        scope: {attr1: "=", attr2: "="},
        link: function(scope, elem, attr){
            console.log("print attributes value: " + attr.attr1 + ", " +  attr.attr2 );
        }
    };

There are different types of bindings you can use:

  • = is for two-way binding
  • @ simply reads the value (one-way binding)
  • & is used to bind functions

See below link for more information: http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-2-isolate-scope

Sign up to request clarification or add additional context in comments.

2 Comments

When I try to execute your code I obtain the following error: Error: scope is not defined. I've tried to pass scope as a parameter controller: function(scope, element, attr){..} but doesn't work.
You are right, I have just updated my example, now it should work. I forgot to pass the "attr" to the link function and then access the attribute that way. Please note that I am using the link function for output and that it should be "restrict: 'E'" instead of "restrict = 'E'".
2

Looking further I found this possible solution to my problem. It is very similar to the one proposed by Plunker with only slight changes in the syntax. This one works for me but I do not understand why the proposed by Plunker not.

app.directive('customDirective', function(){
    return {
        compile: function() {
            return {
                pre: function(scope, element, attributes) {
                    console.log("Value: " + attributes.attr1 + attributes.attr2);
                }
            };
        }
    }
});

Comments

0

Directives can get quite complex. Knowing your end goal would allow a better answer, but this is the simplest way to do what you're asking:

var app = angular.module('test', []);
app.directive("customDirective", function(){
    return {
        link: function(scope, el, attr){
            console.log("print attributes value: " + attr.attr1 + ", " +  attr.attr2 );
        }
    }
});

Plunker

2 Comments

When I execute your code nothing appears in the console neither an error.
@narcispr What version of angular are you using? Did you try the Plunkr? That works...

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.