简体   繁体   English

KendoUI无法在Transclude = true的AngularJS指令中使用

[英]KendoUI not working in AngularJS directive with transclude = true

In this example, I have two AngularJS KendoDatePickers. 在此示例中,我有两个AngularJS KendoDatePickers。 The first one works perfectly, if you click on the button you open the calendar. 如果单击按钮,打开日历,第一个效果很好。 The second one is within a directive that has the transclude attribute set to true . 第二个指令位于将transclude属性设置为true的指令中。 If you click on the second button, you get an error. 如果单击第二个按钮,则会出现错误。

My understanding is that the scope of the transcluded portion inherits from the control scope, so this should work. 我的理解是,被包含部分的范围是继承自控制范围的,因此这应该可以工作。 Where am I wrong? 我哪里错了?

This is the plunk 这是the

HTML HTML

    <input kendo-date-picker="picker" />
    <button ng-click="openPicker()">Open Date Picker</button>

    <my-window>
        <input kendo-date-picker="picker2" />
        <button ng-click="openPicker2()">Open Date Picker 2</button>
    </my-window>

Javascript 使用Javascript

var app = angular.module("app", [ "kendo.directives" ]);

app.controller('MyCtrl',  function($scope) {

     $scope.openPicker = function () {
        $scope.picker.open();
     };


    $scope.openPicker2 = function () {
        $scope.picker2.open();
    };

});


app.directive('myWindow', function() {


  return {
      transclude: true,
      scope: {
          someVar: '='
      },
      restrict: 'EA',
      template: function(element, attrs) {
            var html = '<div ng-transclude></div>';
            return html;
      }
    };
});

There are two things about your code: 您的代码有两件事:

first: you create an isolatedScope so you do not have access to the controller scope inside the directive scope. 第一:您创建了isolatedScope,因此您无权访问指令范围内的控制器范围。

second: transcluded content get their own scope. 第二:被排除的内容获得了自己的范围。 One way to work around this is by not using transclude at all, like the example below: 解决此问题的一种方法是完全不使用transclude,如以下示例所示:

return {
  transclude: false,
  restrict: 'EA',
      template: function(element, attrs) {
        var html = '<div>'+element.html()+'</div>';
        return html;
}

or use the link function and manually transclude the element with the scope of the directive 或使用链接功能,并在指令范围内手动包含元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM