Reputation: 577
Actually,I'm new in angularJS, My scenario is:
$rootScope.synchronization = function($scope, $rootScope, $window, $http)
{
if(localStorage.getItem('job_id') != "" && localStorage.getItem('job_id') > 0)
{
alert("found job id!");
console.log(localStorage.getItem('job_id'));
if(localStorage.getItem('job_id.done_tasks') != "" && localStorage.getItem('job_id.done_tasks') > 0)
{
alert("found done tasks ids!");
console.log(localStorage.getItem('job_id.done_tasks'));
$scope.done_tasks = {};
$scope.done_tasks = localStorage.getItem('job_id.done_tasks');
$scope.job_id = localStorage.getItem('job_id');
console.log($scope.done_tasks);
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)
{
$rootScope.status_id = userdataobject["status_id"];
$rootScope.message = userdataobject["message"];
localStorage.setItem('job_id', '');
localStorage.setItem('job_id.done_tasks', '');
})
}
console.log(localStorage.getItem('job_id'));
console.log(localStorage.getItem('job_id'));
}
};
and in my controller im using this:
$interval( $rootScope.synchronization, 2000 , 1);
Now I'm getting this error:
Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest
and
app.js (line 48)
Error: $http is not a function
$rootScope.synchronization@http://localhost/task-app/js/app.js:52:20
Please help me out to sort this problem out... I have added the $scope, $rootScope, $window, $http but still not worked for me. Have a look on my scenario please.
Upvotes: 0
Views: 774
Reputation: 532
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();
});
Upvotes: 1
Reputation: 170
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.
Upvotes: 1
Reputation: 36
You should change the function ($interval) call This is my suggestion:
$interval($rootScope.synchronization, 2000 , 1, false, $scope, $rootScope, $window, $http);
Better solution for your problem:
$timeout($rootScope.synchronization, 2000, false, $scope, $rootScope, $window, $http);
Upvotes: 1