1

I am trying to list a nested dictionary on html using ng-repeat

Here is the full code

But it seems like I can't access the value of dictionary key by

tree.value

as it either doesn't show up or will only print

'tree.value'

Can someone help me out?

<div ng-app>
  <script type="text/ng-template" id="myTemplate">
      <li>X</li>
      <p>1 tree.value</p>
      <ul ng-repeat="tree in tree.children" ng-include="'myTemplate'">
          <p>2 tree.value</p>
      </ul>
  </script>
 <div>
     <ul ng-include="'myTemplate'"
    ng-init="tree = 
               {
        children: 
            [
                {children: [], value: c12}, 
                {
                    children: 
                        [
                            {children: [], value: c112}, 
                            {children: [], value: c111}
                        ],
                    value: c11
                }
            ], 
        value: c1
    }
"></ul>
  </div>
</div>

2 Answers 2

1

You json is wrongly defined, you should always wrap string value inside single quotes ' like value: c111 & value: 'c112' values should be like value: 'c111' & value: 'c112'

Markup

    <ul ng-include="'myTemplate'"
        ng-init="tree = 
                   {
            children: 
                [
                    {children: [], value: 'c12'}, 
                    {
                        children: 
                            [
                                {children: [], value: 'c112'},
                                {children: [], value: 'c111'}
                            ],
                        value: c11
                    }
                ], 
            value: c1
        }
"></ul>

More better would be define tree variable inside $scope rather than on html.

Working JSFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know why the {{tree.value}} have to be outside of ul tag instead of inside the ul tag?
@Kevin because when <ul ng-repeat="tree in tree.children" ng-include="'myTemplate'"> tag doesn't have tree.children then that ul will not render by ng-repeat that why it should be out side that ul
0

it seems c1 is object not string.so c1 has properties.for example c1 has text property.

you should write in template {{tree.value.text}}

Comments

Your Answer

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.