angularjs - Angular $timeout - TypeError : object is not a function

标签 angularjs

我写了一个指令和一个服务。该指令调用服务中的超时函数。但是,一旦达到 $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/

相关文章:

jquery - 当 $destroy 触发时,是否需要在指令上删除事件处理程序?

javascript - AngularJS - 如何重构我的 Jasmine 规范?

javascript - angularjs ng-repeat 隐藏带有点击事件的元素

javascript - 如何实现 `$locationProvider.html5Mode(true);

javascript - 在 Angular 中,芯片的动态颜色

node.js - Angular js 和 Express js 的基本区别和相似之处是什么?

javascript - 咕噜声 : Location URL app with period

AngularJS - 使用服务和 ng-repeat 时的引用问题

javascript - 使用 javascript 在 160 个字符后增加短信计数器

javascript - ng-options 理解表达式不适用于我的数据