Ameur fahd
Ameur fahd

Reputation: 21

Attributes of a directive with A restrict

Considering a directive having an isolated scope (object), and restrict A, How can I pass their attributes?

For instance, when it comes to restrict E, if scope equals to {attr : '@'} , then the directive will be called like .

Upvotes: 0

Views: 770

Answers (3)

Harpreet
Harpreet

Reputation: 1607

The attributes can be passed in same way as we pass for E type directives.

Refer: http://plnkr.co/edit/T2R91F0iR9GfttSav9Zq?p=preview

//HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example12-production</title>


  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
  <script src="script.js"></script>



</head>
<body ng-app="docsSimpleDirective">
  <div ng-controller="Controller">
  <div my-customer customer="customer" testv="Hello"></div>
</div>
</body>
</html>

//JS
(function(angular) {
  'use strict';
angular.module('docsSimpleDirective', [])
  .controller('Controller', ['$scope', function($scope) {
    $scope.customer = {
      name: 'Naomi',
      address: '1600 Amphitheatre'
    };
  }])
  .directive('myCustomer', function() {
    return {
      restrict: 'A',
      scope: {
        customer:'=',
        testv: '@'
      },
      template: 'Name: {{customer.name}} Address: {{customer.address}} - {{v1}}',
      link: function(scope, element, attrs) {
        console.log(attrs);
        scope.v1=attrs.testv;
      }
    };
  });
})(window.angular);

Upvotes: 1

Shailendra Singh Deol
Shailendra Singh Deol

Reputation: 637

When we create directive with restrict 'E' which stands for 'Element', then it will call like as -

<my-attr-directive attr="somevalue" ></my-attr-directive>

Upvotes: 1

Stepan Suvorov
Stepan Suvorov

Reputation: 26176

the same like with restrict E:

<div my-attr-directive attr="somevalue"></div>

Upvotes: 0

Related Questions