具有A restrict的指令的属性

时间:2016-03-15 09:17:59

标签: javascript angularjs angularjs-directive

考虑具有隔离范围(对象)的指令,并限制A,我如何传递其属性?

例如,当限制E时,如果范围等于{attr:' @'},那么该指令将被调用为。

3 个答案:

答案 0 :(得分:1)

当我们使用restrict' E'创建指令时它代表'元素'然后它将调用如 -

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

答案 1 :(得分:1)

属性的传递方式与传递E类型指令的方式相同。

参考: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);

答案 2 :(得分:0)

限制E

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