Created
June 23, 2013 06:41
-
-
Save sporto/5844051 to your computer and use it in GitHub Desktop.
Angular Nested Directives
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='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