Skip to content

Instantly share code, notes, and snippets.

@sporto
Created June 23, 2013 06:41
Show Gist options
  • Save sporto/5844051 to your computer and use it in GitHub Desktop.
Save sporto/5844051 to your computer and use it in GitHub Desktop.
Angular Nested Directives
<!doctype html>
<html ng-app='NESTED'>
<head>
<script src="components/angular/angular.js"></script>
<script src="js/app/nested.js"></script>
</head>
<body>
<div ng-controller="IndexCtrl">
<collection collection='tasks'></collection>
</div>
</body>
</html>
angular.module('NESTED', [])
.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '='
},
template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"
}
})
.directive('member', function ($compile) {
return {
restrict: "E",
replace: true,
scope: {
member: '='
},
template: "<li>{{member.name}}</li>",
link: function (scope, element, attrs) {
if (scope.member.children && scope.member.children.length) {
//console.log(scope.member.children)
//element.html(element.html() + "<collection collection='member.children'></collection>")
element.append("<collection collection='member.children'></collection>");
$compile(element.contents())(scope)
}
}
}
})
.controller('IndexCtrl', function ($scope) {
$scope.tasks = [{name: 'Sam', children: [{name: 'Sammy'}]}, {name: 'John'}, {name: 'Julia'}];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment