我写了一个指令和一个服务。该指令调用服务中的超时函数。但是,一旦达到 $timeout,就会抛出错误:
TypeError: object is not a function
原装
$scope.msg
不显示。和 $timeout
函数不会等待(20 秒)调用回调(或者我假设,因为 $scope.msg
立即更改)。我完全迷路了。我发现了一些关于超时的问题,但似乎没有一个有这个问题。这个和我得到的一样接近,我已经在关注那个答案 Angularjs directive TypeError: object is not a function .
这是代码的Plunker:
http://plnkr.co/edit/xrepQOWIHlccbW5atW88?p=preview
这是实际的代码。
angular.module('myApp', [
'ui.bootstrap',
'myApp.services',
'myApp.directives',
]);
angular.module('myApp.directives', []).
directive('logoutNotification', [
function(admin) {
return {
restrict: "E",
template: "<div>{{msg}}</div>",
controller: ["$scope", "$timeout", "admin",
function($scope, $timeout, admin) {
$scope.msg = "Hey, no timeout yet";
$scope.resetNotification = function() {
admin.resetNoticeTimer(function(){
$scope.msg = "Hey, I'm back from timeout"
});
};
}
],
link: function(scope) {
scope.resetNotification();
}
};
}
]);
angular.module('myApp.services', []).
factory('admin', ['$rootScope', '$location', '$timeout',
function($rootScope, $timeout, $location, admin) {
var noticeTimer;
return {
resetNoticeTimer: function(cb) {
if (noticeTimer) {
$timeout.cancel(noticeTimer);
}
noticeTimer = $timeout(cb(), 20000);
}
};
}
]);
提前致谢!
最佳答案
您对依赖项进行了错误排序。
您的代码:
factory('admin', ['$rootScope', '$location', '$timeout',
function($rootScope, $timeout, $location, admin) {
应该:
factory('admin', ['$rootScope', '$timeout', '$location',
function($rootScope, $timeout, $location) {
关于angularjs - Angular $timeout - TypeError : object is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272724/