在模板之间切换时,剑道k数据源无法绑定

时间:2015-09-28 16:49:34

标签: javascript angularjs kendo-ui kendo-grid

有没有办法强制kendo小部件绑定到$ scope中的某些数据。我很难让k-data-source在模板加载之间进行绑定。

具体来说,当在角度模板之间切换时,下面的剑道网格中的k数据源不会绑定到$ scope.gridData。但是,当我进行页面刷新时它会绑定。承诺确实解决了,data.data确实保留了我对两种类型的负载的承诺。任何见解将不胜感激。

p.s本期k-data-source not binding不会影响k-column绑定。无论整页加载或角度模板加载,k列都会绑定。

<div kendo-grid="gridK"
k-data-source="gridData"
k-columns="gridColumns"
k-selectable="false"
></div>

.controller('view2Controller', function($scope, tableData) {


//Kendo Grid Data
var promiseGridData = tableData.getData();
promiseGridData.then(function(data){

  $scope.gridData = new kendo.data.ObservableArray(data.data);

});



//Kendo Grid Columns
$scope.gridColumns = [
    {field: "degree", title: "Degree/Certificate Name"},
    {field: "license", title: "License Status"},
    {field: "seekingHelp", title: "Seeking Help"}
];

});


mainView.service('tableData', ['$http', '$q', function($http, $q){

var deferred = $q.defer();
$http.get("../gData.json").then(function(data){

   deferred.resolve(data);
});

this.getData = function()
{   
    return deferred.promise;
}
}]);

以下是应用程序,如果有人想测试它

https://github.com/ssohal/Kendo

您会注意到kendo网格在第一页加载时加载,但在模板之间切换后,它将停止绑定到其数据。

2 个答案:

答案 0 :(得分:0)

以下是mainView应用模块的路线配置...

var mainView = angular.module('mainView', ['ngRoute', "mainView.educationTraining" ]);

mainView.config(["$routeProvider", function($routeProvider){

$routeProvider

    .when('/view2.html', {
        templateUrl: '../Views/view2.html',
        controller: 'view2Controller'
    })

    .otherwise({redirectTo: "../Views/index.html"});

}]);

...这是您的mainView.educationTraining模块的路线配置...

angular.module('mainView.educationTraining', ['ngRoute', 'kendo.directives'])

    .config(['$routeProvider', function($routeProvider) {
    $routeProvider.when("/educationandTraining", {
        templateUrl: "../views/view2.html",
        controller: "educationAndtraining"
    });

}])

第一个引用view2Controller上有gridData个实例的educationAndtraining。第二个引用/view2.html作为控制器。你还没有在github示例中包含第二个控制器的定义,所以我在这里看到的是黑暗。

我认为通过“切换模板”,您的意思是在/educationAndtrainingeducationAndtraining网址之间导航。

如果gridData控制器还没有公开/educationAndtraining的实例,那么当导航到educationAndtraining时,您的网格将无法绑定任何内容,因为视图现在在$scope控制器的educationAndtraining

的上下文中

如果这不正确,请为您的mainView控制器添加一个定义...

我认为您所依赖的是您$scope控制器educationAndtraining$scope控件的<div ng-controller="mainView"> <div ng-controller="educationAndtraining"> </div> </div> 属性的继承。

这适用于这种情况......

  <div ng-repeat="rpt in repeatables.REPEATGRP">
      <input ng-required="false" name="text1" ng-readonly="false" type="text" ng-model="rpt.data.text1" id="text1" value="" />
      <label for="radio1_1" class="radio-inline">
        <input ng-required="true" name="radio1" type="radio" ng-model="rpt.data.radio1" id="radio1_1" value="1" /> Yes
      </label>
      <label for="radio1_2" class="radio-inline">
        <input ng-required="true" name="radio1" type="radio" ng-model="rpt.data.radio1" id="radio1_2" value="0" /> No
      </label><br/><br/>
  </div>

......但不一定在你的情况下。我认为这是因为你的控制器是在完全不同的模块中声明的。

我在github的示例中看不到你的标记,所以请添加更多细节。

您可以通过向两个控制器中注入服务并为该服务中的gridData保留单一事实来源来解决此问题。

如果你能用最简单的小提琴重现这个问题。

答案 1 :(得分:0)

我设置可观察数组的时间太晚了。以下作品:

.controller('view2Controller', function($scope, tableData) {


//Kendo Grid Data
var promiseGridData = tableData.getData();
promiseGridData.then(function(data){

 var arrayhold = data.data;

//Pushes the resolved promise data to kendo observable array
for (var i = 0; idx < arrayhold.length; i++) {
    $scope.gridData.push(arrayhold [i]);
 }

});

可观察数组现在之外因此是先前创建的。当promise被解析时,这个数组将填充并绑定到$ scope。

$scope.gridData = new kendo.data.ObservableArray([]);

//Kendo Grid Columns
$scope.gridColumns = [
{field: "degree", title: "Degree/Certificate Name"},
{field: "license", title: "License Status"},
{field: "seekingHelp", title: "Seeking Help"}
];

});