My callback's below are not able to update/access my $scope variables (or, the changes aren't showing for some reason), how can I achieve this? The callback's are being called, but I think something clearly isn't right with my javascript scoping here.
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);
}
To examine the scope in the debugger: Right click on the element of interest in your browser and select 'inspect element'. You should see the browser debugger with the element you clicked on highlighted. The debugger allows you to access the currently selected element in the console as $0 variable.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.
scope is an AngularJS scope object. element is the jqLite-wrapped element that this directive matches. attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values. controller is the directive's required controller instance(s) or its own controller (if any).
As @charletfi suggested, you need to wrap callbacks from outside Angular in an $apply
to instruct Angular to run a digest cycle to determine changes.
// Should now work
this.updateAnalyticsInitStatus = function (status) {
$scope.$apply(function () {
console.log('boom; ' + status);
$scope.analyticsInitStatus = status;
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With