2014-02-28 89 views

回答

2

我所做的(迄今为止)与此相同的需求是使用Grid上已更改的事件来填充控制器中$ scope的'SelectedRow'对象。对于DetailTemplate,我有一个包含指令的div,该指令从$ templateCache或使用$ http加载模板并编译并将其链接到$ scope。模板的问题之一是编译并将它们链接到$范围以及何时发生的时间。 (我的问题更糟糕,因为我需要每行不同的详细模板)

   $scope.vm.options.productGridOptions = { 
       dataSource: new kendo.data.DataSource({ 
        data: $scope.vm.solution.Products, 
        pageSize: 10 
       }), 
       change: $scope.vm.events.productSelected, 
       columns: $scope.vm.columns.productColumns, 
       detailTemplate: '<div data-template-detail type="#= EntityTemplateSK #"></div>', 
       filterable: false, 
       groupable: false, 
       pageable: true, 
       reorderable: true, 
       resizable: true, 
       selectable: 'single', 
       sortable: true 
      }; 

myApp.directive('templateDetail', ['$compile', '$http', '$templateCache', 
     function ($compile, $http, $templateCache) { 
      var detailTemplateNumbers = ['21', '22', '23', '26', '45', '69']; 
      var getTemplate = function (templateNumber) { 
       var baseUrl = '/App/Product/Views/', 
        templateName = 'productdetail.html', 
        templateUrl = baseUrl + templateName; 

       if (detailTemplateNumbers.filter(function (element) { return element === templateNumber; })[0]) { 
        templateName = 'productTemplate' + templateNumber + '.html'; 
        templateUrl = baseUrl + templateName; 
       } 

       return $http.get(templateUrl, { cache: $templateCache }); 
      }; 

      var linker = function ($scope, element, attrs) { 

       var loader = getTemplate(attrs.type.toString()); 

       if (loader) { 
        loader.success(function (html) { 
         element.html(html); 
        }).then(function() { 
         element.replaceWith($compile(element.html())($scope.$parent)); 
        }); 
       } 
      }; 

      return { 
       restrict: 'A', 
       scope: { 
        type: '=' 
       }, 
       link: linker 
      }; 
    }]); 
+0

无法让此工作。细节指令根本不起作用,不要停在任何断点处。你的改变功能如何?你如何获得模板文件中的数据? – Kungen

+0

加载动态模板的指令使用共享作用域,它是否从网格的父作用域获取其数据。还有其他一些方法来处理这种情况,比如传递它所需的内容,我在其他指令中使用该模式,但是这需要从父范围加载详细视图数据。我已经从我的代码中删除了这个变化事件。数据全部加载到网格绑定的数组上,所以细节视图绑定到'dataItem.detailObject.fieldName'。 – Robharrisaz