이 중첩 된 객체의 값을 렌더링하기 위해 NG 반복을 활용하는 지침을 통해 달성 될 수 appriciated됩니다. 보기 작동 here.
지침
app.directive('divRepeater', function($compile) {
return {
restrict: 'E',
replace: true,
template: '<ul></ul>',
scope: {
obj: '='
},
link: function(scope, element) {
var el = angular.element('<span/>');
el.append('<li>' + scope.obj.id + '</li>');
if (scope.obj.nestedObjs) {
var nestedObjs = angular.toJson(scope.obj.nestedObjs);
/// remove quotes from property names
nestedObjs = nestedObjs.replace(/\"([^(\")"]+)\":/g, "$1:");
var nestedDir = "<div ng-init='nestedObjs=" + nestedObjs + "'><div-repeater ng-repeat='nestedObj in nestedObjs track by $index' obj='nestedObj'></div-repeater></div>";
el.append(nestedDir);
}
$compile(el)(scope);
element.append(el);
}
};
});
컨트롤러
app.controller('MainCtrl', function($scope) {
$scope.objs = [{
id: '1',
nestedObjs: [{
id: 'a'
}, {
id: 'b',
nestedObjs: [{
id: 'i'
}, {
id: 'ii'
}]
}]
}, {
id: '2'
}, {
id: '3',
nestedObjs: [{
id: 'a',
nestedObjs: [{
id: 'i'
}]
}, {
id: 'b',
nestedObjs: [{
id: 'i'
}, {
id: 'ii'
}]
}]
}];
});
마크 업 NG-반복 루핑
<div-repeater ng-repeat="obj in objs track by $index" obj="obj"></div-repeater>
, 당신은하려고 더 좋을 수 있습니다 중첩 된 지시문을 사용하여이를 수행하십시오. http://stackoverflow.com/questions/14430655/recursion-in-angular-directives를 참조하십시오. –