简体   繁体   中英

angular js , ng-model not ready for third party directive

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM