如何使用 jeasyui 将 angular 中的 $scope.mydata 绑定到数据选项?

how to bind $scope.mydata in angular to data-options using jeasyui?

我正在使用来自 jeasyui.com

的 Basic ComboTree

index.js

 $http.get("GetDataForTree")
             .success(function (response) {
                 $scope.Mydata= response;
                 SpinStop();
             });

在 cshtml 中

 <input class="easyui-combotree" 
    data-options="url:'tree_data1.json',method:'get',required:true" style="width:200px;">

how do i bind $scope.Mydata inside data-options ?

您可以创建一个指令[将指令设置为输入元素的属性],并在指令中使用 loadData 在 promise 解析时设置数据。

<input class="easyui-combotree" my-combotree
data-options="url:'tree_data1.json',method:'get',required:true" style="width:200px;">


//Within myCombotree directive link function
link: function link(scope, element, attrs) {
        $http.get("GetDataForTree")
         .success(function (response) {
            //$scope.Mydata= response;
            element.combotree('loadData', response);
             SpinStop();
         });
}

这是工作示例。希望这能解决您的问题...

创建获取远程数据的服务

app.service('treeData', ['$http',function($http){
    this.getData = function(){
        return $http.get('tree_data1.json');
    }
}]);

tree_data1.json数据

[{
    "id":1,
    "text":"My Documents",
    "children":[{
        "id":11,
        "text":"Photos",
        "state":"closed",
        "children":[{
            "id":111,
            "text":"Friend"
        },{
            "id":112,
            "text":"Wife"
        },{
            "id":113,
            "text":"Company"
        }]
    },{
        "id":12,
        "text":"Program Files",
        "children":[{
            "id":121,
            "text":"Intel"
        },{
            "id":122,
            "text":"Java",
            "attributes":{
                "p1":"Custom Attribute1",
                "p2":"Custom Attribute2"
            }
        },{
            "id":123,
            "text":"Microsoft Office"
        },{
            "id":124,
            "text":"Games",
            "checked":true
        }]
    },{
        "id":13,
        "text":"index.html"
    },{
        "id":14,
        "text":"about.html"
    },{
        "id":15,
        "text":"welcome.html"
    }]
}]

创建指令说 "comboTreeDirective" 并将指令作为属性添加到组合树元素

app.directive('comboTreeDirective', function(treeData){
    return {
        restrict: 'A',
        link: function($scope, $elem, $attr){
            treeData.getData().success(function(response){
                $elem.combotree('loadData', response);
            });
        }
    }
   });

使用如下所示的指令

<input class="easyui-combotree" data-options="required:true" style="width:200px;" combo-tree-directive>

下面是完整的工作示例

<!DOCTYPE html>
<html ng-app='myApp'>
<head>
    <meta charset="UTF-8">
    <title>Basic ComboTree - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="themes/icon.css">
    <link rel="stylesheet" type="text/css" href="vendor/demo.css">
    <script type="text/javascript" src="vendor/jquery.min.js"></script>
    <script type="text/javascript" src="vendor/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="vendor/angular/angular.js"></script>
</head>
<body ng-controller="comboTreeCtrl">
    <h2>Basic ComboTree</h2>
    <p>Click the right arrow button to show the tree panel.</p>
    <div style="margin:20px 0"></div>
    <input class="easyui-combotree" data-options="required:true" style="width:200px;" combo-tree-directive>

 <script type="text/javascript">


var app = angular.module('myApp',[]);
app.controller('comboTreeCtrl', function ($scope, $http){

});

app.service('treeData', ['$http',function($http){
    this.getData = function(){
        return $http.get('tree_data1.json');
    }
}]);

app.directive('comboTreeDirective', function(treeData){
    return {
        restrict: 'A',
        link: function($scope, $elem, $attr){
            treeData.getData().success(function(response){
                $elem.combotree('loadData', response);
            });
        }
    }
});
 </script>
</body>
</html>