Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs custom directive with custom attributes

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!

like image 545
narcispr Avatar asked Oct 21 '14 22:10

narcispr


People also ask

What is custom directive in AngularJS?

Custom directives are used in AngularJS to extend the functionality of HTML. Custom directives are defined using "directive" function. A custom directive simply replaces the element for which it is activated.

Can we create custom directive in AngularJS?

AngularJS allows you to create custom directives with which it becomes easier to encapsulate and simplify DOM manipulation in AngularJS. These directives extend the HTML functionality.

What is custom attribute directive in Angular?

Angular has three kinds of directives. Components - is a directive with a template. Structural directives - controls the DOM elements, using which we can add or remove elements from DOM. For ex: *ngIf, *ngFor etc. Attribute directives - to change the behavior or apperance of an element, component or the directive.

How do I create a custom attribute directive?

Steps to create a custom attribute directiveCreate a class decorated with @Directive. Assign the attribute directive name to the selector metadata of @Directive decorator. Use ElementRef class to access DOM to change host element appearance and behavior.


2 Answers

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

like image 87
PzYon Avatar answered Oct 11 '22 23:10

PzYon


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);
                }
            };
        }
    }
});
like image 25
narcispr Avatar answered Oct 12 '22 01:10

narcispr