1

i was reading a write up on directive from this url https://docs.angularjs.org/guide/directive

The restrict option is typically set to:

    'A'     - only matches attribute name
    'E'    - only matches element name
    'C'    - only matches class name
    'M'    - only matches comment

<div ng-controller="Controller">
  <my-customer></my-customer>
</div>

angular.module('docsRestrictDirective', [])
.controller('Controller', ['$scope', function($scope) {
  $scope.customer = {
    name: 'Naomi',
    address: '1600 Amphitheatre'
  };
}])
.directive('myCustomer', function() {
  return {
    restrict: 'E',
    templateUrl: 'my-customer.html'
  };
});

template html file

Name: {{customer.name}} Address: {{customer.address}}

please help me to understand what is the meaning of restrict: 'E', ?

i am looking for a example where restrict will be A or C

please show me the usage of restrict: 'A' and C

also tell me how could i pass multiple argument to directives ?

thanks

1
  • 1
    So you've posted the answer along with the question. What don't you understand - Is it the part of the actual implementation of the directive inside the view? Commented Apr 6, 2016 at 15:09

3 Answers 3

3

let say you have a directive 'myDirective'

if in restrict you have only C you can only use it as classes like this :

<div class="my-directive"></div>

If it's A it's as attribute :

<div my-directive></div>

If it's E it's as element

<my-directive></my-directive>

TO pass argument you generaly se attributes :

<div my-directive my-first-argument="toto" my-second-argument="titi"></div>

To get the value you have to way :

use the attr provided in link function
use the scope with one way or two way binding.

Personnaly i prefer the attribute approach when it comme to directive, element after and class in last. I have already bootstrap based on classes i don't want to clash with it.

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

1 Comment

u said this "use the attr provided in link function use the scope with one way or two way binding." this is not clear to me bcoz i am new in NG. so would discuss it with posting two sample code. one with attr keyword usage and one with scope. looking for further help.
1

please help me to understand what is the meaning of restrict: 'E', ?

The 'E' restriction will match by element name:

<my-customer></my-customer>

i am looking for a example where restrict will be A or C

please show me the usage of restrict: 'A' and C

The 'A' restriction will match by an element attribute:

<div my-customer=""></div>

The 'C' restriction will match by a class:

<div class="my-customer"></div>

also tell me how could i pass multiple argument to directives ?

It depends on your requirements, but one simple way is using an isolated scope:

.directive('myCustomer', function() {
  return {
    restrict: 'E',
    scope: {
       arg1: "@",
       arg2: "@"
    },
    templateUrl: 'my-customer.html'
  };
});

<my-customer arg1="first" arg2="second"></my-customer>

This SO answer gives a pretty good explanation of this process.

6 Comments

just seen your code arg1: "@", is it mandatory to assign scope property with @ sign ? can't it be a empty like arg1: "", please guide. thanks for answer.
what is the meaning of SO you said here.
"@" is a type flag, it means accept a string value. Other options are "=" to bind to another Angular scope variable and "&" to accept a javascript function.
"SO" is a shorthand (lazy) way of typing "StackOverflow".
Sure, this infographic explains the basic differences: onehungrymind.com/… and it links to these examples: plnkr.co/edit/waso3F?p=preview. I found the best way to get a feel for scope isolation to try it out and have an experiment yourself.
|
1

restrict is key to be used in DDO (Directive Definition Object) to inform what it Would be in view

for Example in DDO (Directive Definition Object)

  1. Directive with restrict:E (Element)

    Directive

    app.directive('my-directive',function(){
             return {
                restrict: 'E', //means HTML Element
                ...
              };
    });
    

    so directive in View for restrict:'E' as Element in HTML element as below

view

<myDirective></myDirective>
  1. Directive with restrict:A (Attributes)

Directive

 app.directive('my-directive',function(){
     return {
        restrict: 'A', //means HTML attribute
        ...
      };
     })

so directive in View for restrict:'A' as attribute in HTML element as below

view

<div myDirective></div>
  1. Directive with restrict:'C' (class)

Directive

 app.directive('my-directive',function(){
     return {
        restrict: 'C', //means HTML attribute
        ...
      };
     })

so directive in View for restrict:'C' as class in HTML element as below

view

<div class="myDirective"></div>

you can use Isolate scope (doesn't inherit from its parents scope) or you can pass variables to attributes which depends on your implementation both can used in directive's link and controller

for isolate scope DDO will be as

<my-directive variable1="hello" variable2="world" variable3="call()"></my-directive>



app.directive('myDirective',function(){
         restrict:E,
        scope:{
           variable1:'@variable1',
           variable2:'=varialbe2',
           variable3:'&variable3'
          }
    })

Or you can also pass data through attributes and access them using attrs in Directive's link Function and $attrs DI in Directives Controller

5 Comments

here scope has 3 property call variable1,variable2 and variable3. u assign some with @ sign, some with & sign some with = sign that is not clear. these signs are mandatory ?
u said "you can also pass data through attributes and access them using attrs in Directive's link Function" would post a easy sample code where pass data will be capture by using attrs ? thanks
@Mou Those signs are required if you want isolate scope for your directive , which indeed acts a reusable components which is trivial for large applications.we can pass properties from parent scope to the directive based on the needs. '@' for one-way binding ,'=' for two way binding and '&' method binding.
@Mou for passing data through attributes here is plnkr.co/edit/Y3jBbIQmWWd7oYh0YeR3?p=preview for it
@Mou here is directive with isolated scope plnkr.co/edit/tpl:rfqcl9AHEoJZEEJxyNn2?p=preview

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.