Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up attributes in angularjs directive restricted to comments

Tags:

angularjs

I've found nice feature in angularjs. Directives can be set to work on comments.

{
    ...
    restrict: 'M'
    ...
}

This does the trick as it is spoken in the documentation. Usage of this directive is as follows:

<!-- directive: my-directive-name -->

And it works just fine as long as I don't need to pass arguments to this directive. Is it possible to set arguments on directive restricted to comment? What's the syntax?

like image 881
Szymon Wygnański Avatar asked Oct 29 '12 16:10

Szymon Wygnański


People also ask

What is restrict in AngularJS directive?

Restrict. Angular allows us to set a property named restrict on the object we return on our directive definition. We can pass through a string with certain letters letting Angular know how our directive can be used. function MyDirective() { return { restrict: 'E', template: '<div>Hello world!

What is restrict option in directive?

Directive comes with many Directive Definition Objects (DDO). From them restrict is one. Using restrict option inside a Custom Directive we can prevent the access level of Directive for Element(E), Attribute(A), Comment(M) or Class(C).

Why We Use restrict in AngularJS?

Note: When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option. The restrict option is typically set to: 'A' - only matches attribute name.


1 Answers

<!-- directive: my-directive-name this is all an argument -->

Where everything after the directive name is the value passed into the directive.

app.directive('myDirectiveName', function(){
   return {
      restrict: 'M',
      link: function(scope, elem, attr) {
          alert(attr.myDirectiveName); //alerts "this is all an argument"
      }
   };
});
like image 128
Ben Lesh Avatar answered Oct 23 '22 11:10

Ben Lesh