Created
January 4, 2014 11:25
-
-
Save anonymous/8254405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app='APP'> | |
<head> | |
<meta name="description" content="Angular Nested recursive directives" /> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script> | |
</head> | |
<body> | |
<div ng-controller="IndexCtrl"> | |
<collection collection='tasks'></collection> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('APP', []) | |
.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 (angular.isArray(scope.member.children)) { | |
element.append("<collection collection='member.children'></collection>"); | |
$compile(element.contents())(scope) | |
} | |
} | |
} | |
}) | |
.controller('IndexCtrl', function ($scope) { | |
$scope.tasks = [ | |
{ | |
name: 'Europe', | |
children: [ | |
{ | |
name: 'Italy', | |
children: [ | |
{ | |
name: 'Rome' | |
}, | |
{ | |
name: 'Milan' | |
} | |
] | |
}, | |
{ | |
name: 'Spain' | |
} | |
] | |
}, | |
{ | |
name: 'South America', | |
children: [ | |
{ | |
name: 'Brasil' | |
}, | |
{ | |
name: 'Peru' | |
} | |
] | |
} | |
]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment