Created
July 25, 2016 06:58
-
-
Save tejpratap46/7c6f607b9a109165f5f03e7668118331 to your computer and use it in GitHub Desktop.
Nested comments in Angular 1.x
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' | |
} | |
] | |
} | |
]; | |
}); |
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From:
http://sporto.github.io/blog/2013/06/24/nested-recursive-directives-in-angular/