AngularJS:没有定义在我的全球$ rootscope功能的错误,没有定义$范围 [英] AngularJS: Error in my global function of $rootscope not defined, $scope not defined
问题描述
其实,我在angularJS新的,我的情况是:
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);
现在我得到这个错误:
Error: [$rootScope:inprog] http://errors.angularjs.org/undefined/$rootScope/inprog?p0=%24digest
和
app.js (line 48)
Error: $http is not a function
$rootScope.synchronization@http://localhost/task-app/js/app.js:52:20
请帮我理清这个问题了......我已经加入了$范围,$ rootScope,$窗口,$ HTTP但仍然没有为我工作。对我的情况下看看好吗。
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.
推荐答案
你没有通过正确的值到功能,我可以从你的电话,请参阅:
You're not passing the correct values into the function as I can see from your call:
$间隔($ rootScope.synchronization,2000年,1);
间隔调用你的函数,并且该同步函数需要四个参数:
Interval calls your function, and the synchronization function requires four parameters:
$ rootScope.synchronization =功能($范围,$ rootScope,$窗口,$ HTTP){/ ** /}
您可以传递的参数是这样的:
You can pass the parameters like this:
$间隔($ rootScope.synchronization,2000年,1,假的,范围$,$ rootScope,$窗口,$ HTTP);
请注意在上面的调用假以 $间隔
。这将确保你不在 $适用
周期它解决了错误的:[$ rootScope:inprog]
。
文档为 $间隔
这里
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.
此外,我建议改变这个code到 $超时
为你做它只有一次。此外,你应该使用 $ window.localStorage
而不是的localStorage
。
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
.
编辑2:
基于OP的评论服务将是最好的解决办法:
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();
});
这篇关于AngularJS:没有定义在我的全球$ rootscope功能的错误,没有定义$范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!