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

前端之家收集整理的这篇文章主要介绍了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,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);

}
正如@charletfi建议的那样,你需要在$apply中包装来自Angular外部的回调来指示Angular运行一个摘要周期来确定更改.
// Should now work
this.updateAnalyticsInitStatus = function (status) {
    $scope.$apply(function () {
        console.log('boom; ' + status);
        $scope.analyticsInitStatus = status;
    });
};
原文链接:https://www.f2er.com/angularjs/141469.html

猜你在找的Angularjs相关文章