0

Have a look in this fiddle http://jsfiddle.net/uXbn6/4619/

Here I have used recursive templates for printing my data. What I want is 'Hi' text should only get printed only when correspondind array length is greater than 1.

Here for first array 'tree', as length is greater than 1, Hi should be printed. Now in child when repeat loops on data.nodes, as length of data.nodes is 1 or less than 1, 'Hi' should not be get printed.

In short I want output like this for this example

firstParenNode Hi
node1.1
secondParenNode Hi

<script type="text/ng-template" id="tree_item_renderer.html">
 <div> {{data.name}}
 <span ng-show="tree.length > 1"> Hi </span>
 </div>
 <div ng-repeat="data in data.nodes" ng-include="'tree_item_renderer.html'"></div>
 </script>
<div ng-app="myApp" ng-controller="TreeController">
 <div ng-repeat="data in tree" ng-include="'tree_item_renderer.html'"></div>
</div>
angular.module("myApp", []).
controller("TreeController", ['$scope', function($scope) {
 $scope.tree = [{
 name: "firstParenNode", 
 nodes: [{
 name: "node1.1",
 nodes: []
 }]
 }, {
 name: "secondParenNode",
 nodes: []
 }];
}]);

Any Help would be appreciated.

asked Jan 16, 2018 at 4:32

2 Answers 2

2

Try it:

<script type="text/ng-template" id="tree_item_renderer.html">
 <div> {{node.name}} 
 <span ng-show="node.nodes.length > 0"> Hi </span>
 </div>
 <div ng-repeat="node in node.nodes" ng-inlude="'tree_item_renderer.html'"></div>
</script>
<div ng-app="myApp" ng-controller="TreeController">
 <div ng-repeat="data in tree">
 <div> {{data.name}}
 <span ng-show="tree.length > 1"> Hi </span>
 </div>
 <div ng-repeat="node in data.nodes" ng-include="'tree_item_renderer.html'"></div>
 </div>
</div>

See on my Fiddle: http://jsfiddle.net/uXbn6/4622/

answered Jan 16, 2018 at 5:31
Sign up to request clarification or add additional context in comments.

3 Comments

OP said he wants 'Hi' to print if the node length is greater than 1, not 0. With that in mind this solution prints 'Hi' on a parent node even if there were not more than 1.
He want output "secondParenNode Hi". How it possibly in that conditions? secondParenNode length is not greater than 1
aah, now i understand. I edit my answer. In 12 string add "ng-show = tree.length>1"
1

If I understand correctly, just replace

<span ng-show="tree.length > 1"> Hi </span>

with

<span ng-show="data.nodes.length > 1"> Hi </span>.

answered Jan 16, 2018 at 4:42

7 Comments

But what about parent array, i.e. '$scope.tree', If I replace it, as you suggested it will show Hi only on child nodes not on root node
It should print 'Hi', if the current looping array has length greater than 1
Right now it looks like $scope.tree is not a node itself, but rather an array of objects. Each object is an actual node with a name and an array of child nodes. My suggestion should print what your example shows.
No, its not printing what I have, See in question, I have added desired output in my question, your suggestion is not printing 'Hi' at all
That's because none of the nodes arrays have length > 1. Like I said, $scope.tree is not being printed; it is not a node. Perhaps you want the length of the parent array's nodes instead of current? In which case you'll probably want to change your array structure to have one root node object with a name and nodes array which contains all other nodes.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.