Angular: Directive Transclude
http://angularjs.org/
HTML
<script src="http://code.angularjs.org/1.1.1/angular.min.js"></script>
<script type="text/ng-template" id="directive.html">
<div>
<div>Inside {{scoped}} Directive: {{text || "Undefined"}}</div>
<div>Transcluded {{scoped}}: <span ng-transclude></span></div>
<div ng-repeat="a in as">
Transcluded {{scoped}} within '{{a}}': <span ng-transclude></span>
</div>
</div>
<hr/>
</script>
<div ng-controller="Ctrl">
Outside: {{ text }}
<noscope>{{text || "Undefined"}}</noscope>
<withscope>{{text || "Undefined"}}</withscope>
<withisoscope>{{text || "Undefined"}}</withisoscope>
</div>
JavaScript
var app = angular.module('myApp', []);
function Ctrl($scope) {
$scope.text = "Hello World.";
}
app.directive("noscope", function ($http) {
return {
restrict: 'E',
transclude: true,
templateUrl: 'directive.html',
controller: function ($scope) {
$scope.as = ['1'];
$scope.scoped = "(parent scope " + $scope.$id + ")";
}
};
});
app.directive("withscope", function ($http) {
return {
restrict: 'E',
transclude: true,
templateUrl: 'directive.html',
scope: true,
controller: function ($scope) {
$scope.as = ['1'];
$scope.scoped = "(child scope " + $scope.$id + ")";
}
};
});
app.directive("withisoscope", function ($http) {
return {
restrict: 'E',
transclude: true,
templateUrl: 'directive.html',
scope: {},
controller: function ($scope) {
$scope.as = ['1'];
$scope.scoped = "(isolated scope " + $scope.$id + ")";
}
};
});