Search code examples
javascriptangularjskendo-uikendo-windowkendo-template

Compile kendo window after refresh content


I have a single kendo-window in my page and i want to load content to it every time i click a command. The content i want to load is either from x-kendo-template or from .html with expression in angular {{Sample}}. After the content is loaded, i tried to $compile the content to make use of the angular binding but it is not working. This is what i tried so far

On my markup

<base href="http://demos.telerik.com/kendo-ui/grid/angular">

<script id="tplAddNew" type="text/x-kendo-template">
    Action in Add New: {{ Action }}
</script>

<script id="tplView" type="text/x-kendo-template">
    Action in View: {{ Action }}
</script>

<div id="example" ng-app="Test">
    <div ng-controller="MyCtrl">
        <div kendo-window="winTesting"
            k-visible="false"
            k-modal="true"
            k-pinned="true"
            k-width="'500px'"
            k-min-height="'200px'">
        </div>

      <button 
          kendo-button 
          ng-click="AddEntry()">Add Entry</button>
      <button 
          kendo-button 
          ng-click="ViewContent()">View Content</button>
    </div>
</div>

and here is my controller

var app = angular.module("Test", ["kendo.directives"]);
app.controller("MyCtrl", [
    "$scope", "$compile",
    function($scope, $compile) {
        $scope.AddEntry = function() {
            $scope.Action = "Add New";

            $scope.winTesting
                .refresh({
                    //url: '../References/Test-entry.html'
                    template: kendo.template($('#tplAddNew').html())
                })
                .setOptions({
                    title: "Create New User"
                });

            //$scope.$apply(function() {
            //    $compile($scope.winTesting.element)($scope);
            //});

            $scope.winTesting.open().center();
        }

        $scope.ViewContent = function() {
            $scope.winTesting
                .refresh({
                    //url: '../References/View-entry.html'
                    template: kendo.template($('#tplView').html())
                })
                .setOptions({
                    title: "View User Detail"
                });

            //$scope.$apply(function() {
            //    $compile($scope.winTesting.element)($scope);
            //});

            $scope.winTesting.open().center();
        }
    }
]);

lets assume that the content of Test-entry.html is the same as tplAddNew Template

Now, when i will use the $compile function, it will show an error of $apply already in progress

Any help would be appreciated. TIA

i also prepared a JSFiddle


Solution

  • Kendo has not really refined this widget yet. So in light of that use the $timeout service:

    $timeout(function() {
      $compile($scope.winTesting.element)($scope);
    }, true);
    

    The last boolean parameter does (more or less) a $scope.$apply. See the Documentation

    Edit:

    There is also some cool stuff you could do with the $parse service as well. You might find it useful as explained in this blog