0

I am using https://github.com/ezraroi/ngJsTree and I have come across the following problem.

My ng-model is a AJAX request and when my controller fires, data is not yet ready, so it does not render anything.

<div ng-controller='myCtrl'>
    <div js-tree="treeConfig" ng-model="treeData" should-apply="ignoreModelChanges()" tree="treeInstance" tree-events="ready:readyCB;create_node:createNodeCB"></div>
</div>

https://plnkr.co/edit/IRaqvd0DPcqkxgo0xoqa?p=preview

Justin Homes
  • 3,739
  • 9
  • 49
  • 78
  • if you are using `$http` for your server calls, the directive should update properly when the response returns. perhaps your problem is with the way your service calls are happening. Showing that code would help focus the problem a bit to find a solution. – Claies Oct 16 '16 at 19:00
  • also, `ignoreModelChanges()` seems an odd thing to pair with an `$http` request where the model will definitely change; have you tried to see if it works when this is removed? – Claies Oct 16 '16 at 19:02
  • here is my pluker https://plnkr.co/edit/fCrupsxDMprHdLCocZIe?p=preview – Justin Homes Oct 16 '16 at 20:46
  • your problem seems to be due to your scripts not loading properly. See the console errors: "Refused to execute script from 'https://raw.githubusercontent.com/vakata/jstree/master/dist/jstree.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled." GitHub does not support linking `raw` scripts directly. – Claies Oct 16 '16 at 20:58
  • i updated my pluker https://plnkr.co/edit/IRaqvd0DPcqkxgo0xoqa?p=preview – Justin Homes Oct 16 '16 at 22:14
  • your plunker was missing an `ng-app` declaration, it wasn't even bootstrapping. – Claies Oct 16 '16 at 23:28
  • added it , but still same result – Justin Homes Oct 17 '16 at 02:48

1 Answers1

0

Depending on the structure of your application, you may have a few alternatives to handle this situation. Create a service that is responsible for reading the data. Then you can do one of the following:

  1. make router to wait till the reading is resolved before navigating to the view. Here is a related SO question: AngularJS $http.get with resolve

  2. add event listener to the controller and make service to emit/broadcast event when the data is ready.

In both cases, you may want to read data from service.

Community
  • 1
  • 1
Vladimir M
  • 4,403
  • 1
  • 19
  • 24
  • any example of bulltet 1 and 2? – Justin Homes Oct 16 '16 at 19:06
  • added a link to the SO question about resolving the data in the routeProvider. As for 2nd example - I'd go for angular documentation but there is also related SO question. (http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on) – Vladimir M Oct 16 '16 at 19:20