<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css">
<script src="jquery.min.js"></script>
<script src="angular.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var app = angular.module('myapp', []);
app.directive('cust', function(){
return {
restrict: 'EA',
transclude: true,
replace: true,
link: function(scope, elem, attrs, ctrl, transclude) {
transclude(scope, function(clone) {
console.log(clone);
});
},
template:"<span ng-transclude></span>"
}
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<cust><div>hello</div></cust>
</body>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css">
<script src="jquery.min.js"></script>
<script src="angular.js"></script>
<script src="bootstrap.min.js"></script>
<script type="text/javascript">
var app = angular.module('myapp', []);
app.directive('cust', function(){
return {
restrict: 'EA',
transclude: true,
replace: true,
link: function(scope, elem, attrs, ctrl, transclude) {
transclude(scope, function(clone) {
console.log(clone);
});
},
template:"<span ng-transclude></span>"
}
});
</script>
<style type="text/css">
</style>
</head>
<body ng-app="myapp">
<cust><div>hello</div></cust>
</body>
</html>
可以看到link函数的第五个参数是一个函数,它的第二个参数也是一个函数,其中clone就是那部分要被嵌入template中的内容<div>hello</div>.