Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access my angular $scope inside my custom callback?

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);

}
like image 963
williamsandonz Avatar asked Dec 01 '13 23:12

williamsandonz


People also ask

How do you access $scope in console?

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.

How does $scope work in angular?

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).

What is the scope of $scope in AngularJS?

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.

What is attrs in AngularJS?

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).


1 Answers

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;
    });
};
like image 78
Kevin Stone Avatar answered Oct 15 '22 04:10

Kevin Stone