2017-05-31 68 views
1

到目前为止,我已经完成了我所需要的非常有限的前端技能的90%,因此我现在需要帮助。从角度变量填充jstree threeview

我使用jstree构建treview,它对静态数据(else块中的那个)的工作正常,如下所示。

<script> 
    $(function() { 
     $("#list").jstree({ 
      "core": { 
       "data": function (node, cb) { 
        if (node.id === "#") { 
         cb([{ 
          "text": "Root", 
          "state": {"opened": true}, 
          "children": true 
         }]); 
        } else { 
         // This is the one I am try to populate dynamicaly 
         cb([{"id":"1","text":"Lis 1"},{"id":"2","text":"Lis 2"}]); 
        } 
       } 
      } 
     }); 
    }); 
</script> 

不过,我现在需要填充else块与动态结果集未来的角度可变vm.list可用{{ vm.list }}在HTML页面转储),所以我该怎么办呢?该文档有一些关于动态人口数据的细节(html,array/json,ajax,lazy loadcallback),但没有像我需要的。

通过vm作为回调函数中的第三参数,并将else块更改为cb(vm.cat);没有成功。如果我的前端知识不在最低端,我会想出更好的尝试!

我的角度控制器的样子:

'use strict'; 

module.exports = ['$on', '$timeout', 'listService', 
    function($on, $timeout, listService) { 
     var vm = this; 
     .... 
     load(); 
     .... 

     function load() { 
      // This is actually coming from an API and massive 
      vm.cat = [{'id':'1', 'text':'Lis 1'}, {'id':'2', 'text':'Lis 2'}]; 
     }; 
    } 
]; 

回答

0

我有一个有效的解决方案,但不喜欢它,所以我就等着有人用更好的解决办法,否则我会遗憾地接受这个,因为它转储到内容一个DOM元素并读取它的内容来创建树,在我看来这有点冒险和沉重。

<div id="data" style="display: none">{{ vm.cat }}</div> 


$(function() { 
    var data = $('#data').text(); 
    if (data) { 
     $("#list").jstree({ 
      "core": { 
       "data": JSON.parse(data) 
      } 
     }); 
    } 
});