2016-04-05 16 views
0

我的ES6控制器类如下。我无法执行代码的promise解析部分中的$ compile,$ location等Angular方法。它引发错误“Uncaught(in promise)TypeError:this。$ compile不是一个函数(...)”我正在使用箭头函数来获取promise中的这个访问。任何想法如何解决这个问题?

import DashboardService from './DashboardService'; 

class DashboardController 
{ 
    /*@ngInject*/ 
    constructor($scope, DashboardService, $location, $compile, PLATFORM) 
    { 
     this.$scope = $scope; 
     this.DashboardService = DashboardService; 
     this.$location = $location; 
     this.$compile = $compile; 
     this.PLATFORM = PLATFORM; 
    } 

    UpdateGrid(clients) 
    { 
     this.PLATFORM.miraLoader.moduleImport('platform!kendo-ui').then((kendo) => {   
      var dataSource = new kendo.data.DataSource({ data: clients, pageSize: 10 }); 
      angular.element("#GridTest").kendoGrid({ 
       height: 415, 
       scrollable: true, 
       dataBound: function(){this.$compile(angular.element("#GridTest"))(this.$scope);} 
      }); 
      var grid = angular.element("#GridTest").data("kendoGrid"); 
      grid.setDataSource(dataSource); 
      grid.dataSource.read(); 
      this.$compile(angular.element("#GridTest"))(this.$scope); 
     }); 
    } 

    GoToClient(id){ this.$location.path('/Client/'+id); } 

    AdvisorChange() { 
     this.DashboardService.ClientsGet(this.wiquid, this.advisorid).then((clients) => { 
      this.UpdateGrid(clients.data.d); 
     }); 
    } 
} 
export default DashboardController; 

回答

0

箭头操作,确保您可以访问this.$compile()的成功回调中为then,但是你想从另一个回调dataBound内访问它。

您可以执行以下操作之一。在UpdateGrid

const vm=this 

然后在回拨电话vm.$compile(...)

或使用:

dataBound: (function(){this.$compile(angular.element("#GridTest"))(this.$scope);}).bind(this) 

迫使thisdataBound内回调的预期值。

或者你可以把:

const $compile = this.$compile 

UpdateGrid并调用从回调$compile(...)

+0

谢谢邓肯..现在工作正常... –