Kendo Grid:在Angular中获取小部件实例 [英] Kendo Grid: Getting widget instance in Angular

查看:77
本文介绍了Kendo Grid:在Angular中获取小部件实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Angular Controller中获取Kendo网格的实例,因此我可以尝试挂接一些事件(和调用方法),我知道这可能不是最佳实践(应该使用自定义指令) ),但根据文档,我们应该可以使用...

I was trying to get an instance of the Kendo grid within my Angular Controller, so I can try and hook up to some events (and call methods) I know this is probably not best practice (and should probably use a custom directive), but according to the documentation, we should be able to use...

<div ng-app="app" ng-controller="MyCtrl">
<input kendo-datepicker="datePicker" k-on-change="onChange()">
</div>
<script>
 angular.module("app", [ "kendo.directives" ]).controller("MyCtrl",
   function($scope) {
 $scope.onChange = function() {
 alert($scope.datePicker.value());
};

});

因此,我正在尝试对网格进行同样的操作.我有以下标记...

So, I was trying to do the same with the grid. I have the following markup...

<div ng-controller="Grid">
  <div kendo-grid='grid' k-options="vm.gridOptions"></div>  
</div>

,然后在控制器js文件中.

and then in the controller js file..

  angular
    .module("mygrid")
    .controller("Grid", ['$scope', Grid]);

    function Grid($scope) {             
      var gridInstance = $scope.grid;
      ...

此处

但是,gridInstance始终是未定义的.有人知道我是否应该使用网格来做到这一点,如果可以,为什么上面的内容总是返回undefined?

However, gridInstance is always undefined. Does anyone know if I shoud be able to do this with the grid, and if so why the above always returns undefined?

在此先感谢您的帮助

彼得

推荐答案

两个问题:

  1. 如果使用"controller as"语法,则需要为要访问的内容添加前缀(在您的情况下,您需要kendo-grid="vm.grid"而不是kendo-grid="grid"
  2. 实例化控制器时,还不存在Kendo UI小部件(类似的问题在此),所以您需要使用全局Kendo UI事件等待它
  1. If you use the "controller as" syntax, you need to prefix things you want to access (in your case, you need kendo-grid="vm.grid" instead of kendo-grid="grid"
  2. When your controller is instantiated, the Kendo UI widget doesn't exist yet (similar question here), so you need to wait on it using a global Kendo UI event

因此您的HTML变为:

So your Html becomes:

<div data-ng-app="app">
    <div data-ng-controller="Grid as vm">
        <div kendo-grid="vm.grid" k-options="vm.options"></div>
        <div>{{vm.msg}}</div>
    </div>
</div>

您的应用程序:

(function () {
    angular.module("app", ["kendo.directives"])
        .controller("Grid", ["$scope", Grid]);

    function Grid($scope) {
        var vm = this;

        var gridData = [{
            col1: 'data1',
            col2: 'data2'
        }, {
            col1: 'data1',
            col2: 'data2'
        }];

        vm.options = {
            dataSource: gridData,
            editable: true
        };

        $scope.$on("kendoRendered", function (event) {
            var gridInstance = vm.grid;
            console.log(vm);
            vm.msg = gridInstance === undefined ? "undefined" : "defined";
        });
    }
})();

(更新的演示)

这篇关于Kendo Grid:在Angular中获取小部件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆