3

I'm using the ui-tree in angularjs. For binding I use a method which gives me the child elements:

<div ui-tree="treeOptions">
    <ol ng-init="children=getChildren(website.id, '0')" ng-model="children" ui-tree-nodes>
        <li ng-repeat="node in children" data-collapsed="node.isCollapsed" ui-tree-node ng-include="'nodeTemplate.html'" ng-cloak></li>
    </ol>
</div>

With ng-init I initialize the children array for further usage. This code works, but ng-init is not reevaluated when data changes. So dynamically adding a new item is not possible. Is there a way of setting a local variable in HTML similar to ng-init that gets refreshed automatically?

scniro
  • 16,844
  • 8
  • 62
  • 106
MichaelS
  • 3,809
  • 2
  • 26
  • 33
  • `ng-init` parsed once.. If you're calling it in `ng-repeat` then it will be executed with each iteration – Vineet Jul 14 '15 at 18:42
  • @Vineet yes, thats right. But after everything is rendered (`ng-repeat` is finish) then `ng-init` is never called again. – MichaelS Jul 14 '15 at 18:51
  • `ng-init` is out the `ng-repeat` scope, I guess – Vineet Jul 14 '15 at 18:55
  • why don't you put call to `getChildren` in controller? `ng-model` itself is refreshed automatically, basically it creates a watch. So do you really have an event of data being changed? – Kirill Slatin Jul 14 '15 at 19:28
  • @KirillSlatin I will do that when there is no other possibility. – MichaelS Jul 14 '15 at 20:36
  • The real question is how do you get your data? If `getChildren` is an AJAX request and the only way to know that data is updated is to poll the server regularly, then you definitely need to do that in controller where you can control the frequency. All other directives that are not parsed once are parsed each (!) $digest cycle for several times and that happens pretty frequently – Kirill Slatin Jul 14 '15 at 20:38

0 Answers0