I'm trying to create a nested directive with angularjs.
This is my code: (http://jsfiddle.net/TH55L/)
JS:
myApp.directive('nodo', function ($compile) {
return {
restrict: 'E',
terminal: true,
scope: {
n: "="
},
link: function (scope, element, attrs) {
var template = "<p>{{n.name}}</p>";
if (scope.n.nodo != null) {
template += '<nodo n="n.nodo"></nodo>';
}
var newElement = angular.element(template);
$compile(newElement)(scope);
element.replaceWith(newElement);
}
}
})
function MyCtrl($scope) {
$scope.nodo = {
"name": "Level 0",
"nodo": {
"name": "Level 1",
"nodo": {
"name": "Level 2",
"nodo": {
"name": "Level 3",
"nodo": null
}
}
}
};
}
HTML:
<div ng-controller="MyCtrl">
<nodo n="nodo"></nodo>
</div>
The expected result is this:
<p>Level 0</p><p>Level 1</p><p>Level 2</p><p>Level 3</p>
But i'm only getting this:
<p>Level 0</p>
It's like the $compile(newElement)(scope) not recognizes the directive.
Here is an example in Knockoutjs of what I wanna do: http://jsfiddle.net/y64rY/