我写了一个指令和一个服务.该指令在服务中调用超时函数.但是,一旦达到$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,$location,admin) { var noticeTimer; return { resetNoticeTimer: function(cb) { if (noticeTimer) { $timeout.cancel(noticeTimer); } noticeTimer = $timeout(cb(),20000); } }; } ]);
提前致谢!
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。