Kendo UI and angular - no widget in $scope

旧城冷巷雨未停 提交于 2019-12-04 10:04:53

I have found out the problem myself, so I'm going to post an answer if someone has the same problem. If you use the controllerAs syntax in AngularJS, you can't just write the name of the widget - you have to prefix it with your controller alias.

Take a look at this example:

<div ng-controller="MyController as ctrl">
    <div kendo-grid="myGridName"></div>
</div>

This will not give you the grid object on the $scope - for that you need to add the ctrl prefix:

<div ng-controller="MyController as ctrl">
    <div kendo-grid="ctrl.myGridName"></div>
</div>

Now you have access to the widget in your controller like this:

angular.module('MyModule',['kendo.directives'])
    .controller('MyController', function($scope){
        // this gives you the widget object
        console.log(this.myGridName);

        // however, this doesn't work
        console.log($scope.myGridName);
});

I hope I helped someone with this post. Cheers,

Try waiting for the event that Kendo emits.

Html

<div kendo-grid="grid" options="gridOptions"></div>

Javascript

$scope.$on("kendoWidgetCreated", function(event, widget){
    if (widget === $scope.grid) {
        console.log($scope.grid);
    }
});

Edit: See this Plunker

It is because angular is too fast and the kendo element doesn't exist when you try to set the options.

I solved this with a watch.

This is my html code:

<div kendo-grid="ListDesign"></div>

And this is my angular code

 $scope.$watch('ListDesign', function () {
        if ($scope.ListDesign != undefined) {
            var gridOptions = {
                columns: columns,
                sortable: {
                    mode: 'multiple',
                    allowUnsort: true
                }
            };
            $scope.ListDesign.setOptions(gridOptions);
            $scope.ListDesign.setDataSource(dataSource);
        }
    });

Does this answer your question?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!