を追加します。私は、次のディレクティブを使用しています:角度は、私はこのような階層データを持つ親の属性値
app.directive('tree', function (hierarchyService, logger, $timeout) {
return {
scope: {
data: '='
},
restrict: 'E',
replace: true,
template: '<div>' +
'<table class="table table-striped table-hover">' +
' <thead>' +
' <tr>' +
' <th class="col-md-6">Account name</th>' +
' </tr>' +
' </thead>' +
' <tbody><tr collection data="data" /></tbody>' +
' </table>' +
'</div>'
};
});
app.directive('collection', function() {
return {
restrict: "A",
replace: true,
scope: {
data: '=',
depth: '@'
},
template: '<member ng-repeat="member in data" member="member" depth="{{depth}}" />',
link: function (scope, element, attrs) {
scope.depth = parseInt(scope.depth || 0);
}
}
});
app.directive('member', function($compile) {
return {
restrict: "E",
replace: true,
scope: {
depth: '@',
member: '='
},
template: '<tr ng-class="{selected: member.selected}">' +
'<td>' +
' <span ng-show="depth > 0" style="width: {{depth * 16}}px; display: inline-block;"></span> {{member.name}}' +
'</td>' +
'</tr>',
link: function (scope, element, attrs) {
scope.depth = parseInt(scope.depth || 0);
if (angular.isArray(scope.member.children) && scope.member.children.length > 0) {
var el = angular.element('<tr collection data="member.children" depth="{{newDepth}}" />');
scope.depth = parseInt(scope.depth || 0);
scope.newDepth = scope.depth + 1;
$compile(el)(scope);
// Flatten hierarchy, by appending el to parent
element.parent().append(el);
}
}
}
});
問題は、親スコープからlink
方法、depth
から追加されたコレクションにnewDepth
に追加される、ということです。その結果、レベル3ノードのdepth
の値はdepth="3 2 1 "
になります。
depth
の継承を無効にする方法はありますか?
また、私はcollection
とmember
ディレクティブでfalseにreplace
を変更すると、意図したとおりの深さは動作しますが、その後、HTMLが無効であること、気づきました。
は、あなたがあなたのケースのためにplunkerやフィドルを提供してもらえますか? –
@SpartakLalajここにあります:https://plnkr.co/edit/CE2z5WeU41H4qJQQ73B7?p=preview – Krzysztof
深度属性を削除し、等号記号で宣言しました。数字が正しいようです。それが正しい結果であるかどうか試してみてください! –