Skip to content Skip to sidebar Skip to footer

Angularjs: Error In My Global Function Of $rootscope Not Defined, $scope Not Defined

Actually,I'm new in angularJS, My scenario is: $rootScope.synchronization = function($scope, $rootScope, $window, $http) { if(localStorage.getItem('job_id') != '' && l

Solution 1:

You should change the function ($interval) call This is my suggestion:

$interval($rootScope.synchronization, 2000 , 1, false, $scope, $rootScope, $window, $http);

$inverval API

Better solution for your problem:

$timeout($rootScope.synchronization, 2000, false, $scope, $rootScope, $window, $http);

$timeout API

Solution 2:

You can define this synchronization function in your controller, I dont think you need to use rootscope at all. You can use this controller;

yourApp.controller("yourController", ['$scope', '$http', '$timeout', 'localStorage',
    function ($scope, $http, $timeout, localStorage) {

        var synchronization = function () {
            if (localStorage.getItem('job_id') != "" && localStorage.getItem('job_id') > 0) {

                if (localStorage.getItem('job_id.done_tasks') != "" && localStorage.getItem('job_id.done_tasks') > 0) {
                    $scope.done_tasks = {};
                    $scope.done_tasks = localStorage.getItem('job_id.done_tasks');
                    $scope.job_id = localStorage.getItem('job_id');

                    var userData = $http({
                        method: "post",
                        url: "http://localhost/t-app/mobile-data/update-tasks.php",
                        data: {
                            done_tasks: $scope.done_tasks,
                            job_id: $scope.job_id
                        },
                        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                    });
                    userData.success(function (userdataobject) {
                        //* I added response to $scope *//$scope.status_id = userdataobject["status_id"];
                        $scope.message = userdataobject["message"];

                        localStorage.setItem('job_id', '');
                        localStorage.setItem('job_id.done_tasks', '');

                        $timeout(synchronization, 2000);
                    });
                }
            }
        };

    }]);

Just copied your function to controller and arrenged injections..

Note: I used timeout service to recall this function every 2 sec instead of interval, you can tweak that according to your need, carrying the line out of synchronization function will call it once after 2 sec.

Solution 3:

You're not passing the correct values into the function as I can see from your call:

$interval( $rootScope.synchronization, 2000 , 1);

Interval calls your function, and the synchronization function requires four parameters:

$rootScope.synchronization = function($scope, $rootScope, $window, $http){/**/}

You can pass the parameters like this:

$interval( $rootScope.synchronization, 2000 , 1, false, $scope, $rootScope, $window, $http);

Note the false in the above call to $interval. This will ensure that you're out of the $apply cycle which solves the Error: [$rootScope:inprog]. Docs for $interval are here.

Also I would suggest changing this code to $timeout as you're doing it only once. Besides, that you should use $window.localStorage instead of localStorage.

Edit 2: Based on OP's comment a service would be the best solution:

myApp.factory('SynchronizationService', ['$scope', '$rootScope',
    '$window', '$location', '$interval', '$http',
    function ($scope, $rootScope, $window, $location, $interval, $http) {
        return {
            synchronization: function () { /*your code here*/}
        };
    }]);

Then you can call this from a controller by injecting the service, and calling the exposed method:

controller('mainCtrl', ['SynchronizationService', function(SyncService) {
    SyncService.synchronization();
});

Post a Comment for "Angularjs: Error In My Global Function Of $rootscope Not Defined, $scope Not Defined"