我的回调下面无法更新/访问我的$scope变量(或者,由于某些原因,更改没有显示),我该如何实现?
正在调用回调函数,但我认为这里的 javascript范围显然不对.
正在调用回调函数,但我认为这里的 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运行一个摘要周期来确定更改.
原文链接:https://www.f2er.com/angularjs/141469.html// Should now work this.updateAnalyticsInitStatus = function (status) { $scope.$apply(function () { console.log('boom; ' + status); $scope.analyticsInitStatus = status; }); };