ES6 Angular Controller类 - 无法访问$ compile,$ location等

时间:2016-04-05 14:26:13

标签: angularjs ecmascript-6

我的ES6 Controller类如下。我无法执行Angular方法,例如$ compile,$ location来自promise解析部分代码。它抛出错误" Uncaught(在promise中)TypeError:this。$ compile不是函数(...)"我正在使用箭头函数来获取内部承诺的访问权限。知道如何解决这个问题吗?

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;

1 个答案:

答案 0 :(得分: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
<{1>}中的

,只需从回调中调用UpdateGrid