如何在我的自定义回调中访问我的角度$ scope?

时间:2013-12-01 23:06:35

标签: angularjs scope

我的回调下面无法更新/访问我的$ scope变量(或者,由于某些原因,更改没有显示),我该如何实现? 正在调用回调函数,但我认为这里的javascript范围显然不对。

function ReportsCtrl($scope) {
    var self = this;
    $scope.analyticsIsReady = false;
    $scope.analyticsInitStatus = '';

    //$scope.originCompositionChart = new Chart__('originCompositionChart', reportsClient.getOriginCompositionData, true, false);
    $scope.originCompositionChart = new Chart__('originCompositionChart', null, true, false);
    $scope.charts = new Array($scope.originCompositionChart);

    $scope.showIncludedCharts = function () {
    };

    //todo not working
    this.updateAnalyticsInitStatus = function (status) {
        console.log('boom; ' + status);
        $scope.analyticsInitStatus = status;
    };

    //todo not working
    this.handleAnalyticsInitSuccess = function (status) {
        $scope.analyticsInitStatus = 'Initialisation complete';
        $scope.analyticsIsReady = true;
    };

    window.analyticsInitialiserClient = new AnalyticsInitialiserClient__(this.updateAnalyticsInitStatus, this.handleAnalyticsInitSuccess);

}

1 个答案:

答案 0 :(得分:14)

正如@charletfi建议的那样,你需要在$apply中包装来自Angular外部的回调来指示Angular运行一个摘要周期来确定更改。

// Should now work
this.updateAnalyticsInitStatus = function (status) {
    $scope.$apply(function () {
        console.log('boom; ' + status);
        $scope.analyticsInitStatus = status;
    });
};