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

Mou
  • 15,673
  • 43
  • 156
  • 275
  • 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? – Alon Eitan Apr 06 '16 at 15:09

3 Answers3

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.

Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • 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. – Mou Apr 06 '16 at 18:14
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.

Community
  • 1
  • 1
seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • 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. – Mou Apr 06 '16 at 18:08
  • what is the meaning of `SO` you said here. – Mou Apr 06 '16 at 18:16
  • "@" 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. – seanhodges Apr 07 '16 at 12:48
  • "SO" is a shorthand (lazy) way of typing "StackOverflow". – seanhodges Apr 07 '16 at 12:49
  • one request can u redirect me to article which discuss the usage of `@ = &` in much details with many examples. – Mou Apr 07 '16 at 12:52
  • 1
    Sure, this infographic explains the basic differences: http://onehungrymind.com/infographic-understanding-angularjs-isolated-scope/ and it links to these examples: http://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. – seanhodges Apr 07 '16 at 12:57
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

Shushanth Pallegar
  • 2,832
  • 1
  • 14
  • 16
  • 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 ? – Mou Apr 06 '16 at 18:11
  • 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 Apr 06 '16 at 18:12
  • @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. – Shushanth Pallegar Apr 06 '16 at 18:33
  • @Mou for passing data through attributes here is https://plnkr.co/edit/Y3jBbIQmWWd7oYh0YeR3?p=preview for it – Shushanth Pallegar Apr 06 '16 at 18:34
  • @Mou here is directive with isolated scope http://plnkr.co/edit/tpl:rfqcl9AHEoJZEEJxyNn2?p=preview – Shushanth Pallegar Apr 06 '16 at 19:00