2016-03-15 48 views
0

考虑到具有隔离范围(对象)和限制A的指令,如何传递它们的属性?例如,当涉及限制E时,如果范围等于{attr:'@'},那么该指令将被称为like。带有限制的指令的属性

回答

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); 
0

同样喜欢用限制è

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

当我们创建与限制 'E',这代表 '元' 指令,那么它会调用等为 -

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