Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo UI and angular - no widget in $scope

I'm using Kendo UI version 2014.2.716 with AngularJS version 1.2.27, and I made a grid using a directive

<div ng-controller="MyController as ctrl">
    <div id="myGrid" kendo-grid k-options="{some options}"></div>
    <button ng-click="ctrl.doSomething()"></div>
</div>

I read that if you give a name to the grid (like this: kendo-grid="myGridOnScope"), you can access the widget in the controller scope in this way:

myModule.controller('MyController', function($scope) {
   this.doSomething = function() {
       console.log($scope.myGridOnScope);
   }
}

The console.log should log a widget object, but in my case it's undefined. What am I doing wrong? Thanks for the help

like image 987
Mateo Velenik Avatar asked Dec 04 '22 04:12

Mateo Velenik


1 Answers

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,

like image 120
Mateo Velenik Avatar answered Jan 19 '23 10:01

Mateo Velenik