类型错误:未定义无法读取属性“默认prevented” [英] TypeError: Cannot read property 'defaultPrevented' of undefined

查看:299
本文介绍了类型错误:未定义无法读取属性“默认prevented”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误,当我做如下测试:

I am getting this error when I do the following test:

it('should call pauseAnimationInterval if in focus', inject(function(SearchBoxData, intervalManager, $timeout){
  SearchBoxData.init_array = [];
  SearchBoxData.inFocus = true;
  SearchBoxData.init(intervalManager);
  console.log(intervalManager.pauseTimeout);
  console.log(intervalManager.pauseTimeoutTime);
  console.log($timeout);
  $timeout.flush(intervalManager.pauseTimeoutTime+1);
  expect(rootScope.$broadcast).toHaveBeenCalledWith('onPauseInterval', intervalManager.loopIndex);
}));

它打破对 $ timeout.flush(intervalManager.pauseTimeoutTime + 1);
在$超时方法是所谓的 intervalManager.pauseAnimationInterval()这里面 SearchBoxData.init(intervalManager)

intervalManager.pauseAnimationInterval = function (){

  intervalManager.safeCancel(intervalManager.continueInterval);
  intervalManager.safeCancel(intervalManager.initInterval);
  intervalManager.initInterval = null;
  intervalManager.continueInterval = null;

  intervalManager.pauseTimeout = $timeout(function () {
    if(intervalManager.inFocus === true){
      intervalManager.loopIndex += 1;
      if(intervalManager.loopIndex >= intervalManager.maxLoopIndex){
        intervalManager.loopIndex = 0;
      }
      $rootScope.$broadcast("onPauseInterval", intervalManager.loopIndex);
      intervalManager.continueAnimationInterval();
    }else{
      // Important condition: retry after the timeout if no focus
      // main reason of glitch
      intervalManager.pauseAnimationInterval();
    }
  }, intervalManager.pauseTimeoutTime);

};

我之前没有收到此错误,我不知道我做错了什么。

I was not getting this error before, I am not sure what I did wrong.

更新:这里是angularJS的unminified版本堆栈跟踪:

Update: Here is a stacktrace with unminified version of angularJS:

at /Users/foo/projects/bar/vendor/assets/javascripts/angular.js:9268:88
at Scope.$eval (/Users/foo/projects/bar/vendor/assets/javascripts/angular.js:11986:28)
at Scope.$digest (/Users/foo/projects/bar/vendor/assets/javascripts/angular.js:11812:31)
at Scope.$apply (/Users/foo/projects/bar/vendor/assets/javascripts/angular.js:12092:24)
at Object.fn (/Users/foo/projects/bar/vendor/assets/javascripts/angular.js:13627:36)
at Function.self.defer.flush (/Users/foo/projects/bar/spec/javascripts/lib/angular/angular-mocks.js:126:32)
at Function.$delegate.flush (/Users/foo/projects/bar/spec/javascripts/lib/angular/angular-mocks.js:1689:20)
at null.<anonymous> (/Users/foo/projects/bar/spec/javascripts/unit/services/searchboxdata_service_spec.js:71:16)

更新2:我追查回 SearchBoxData 工厂的注入。我不知道什么是错的。简单地注入它会导致每当我创建一个$超时实例,并使用flush方法错误。

Update 2: I traced it back to the injection of the SearchBoxData factory. I am not sure what is wrong with it. Simply injecting it causes the error whenever I create a $timeout instance and use the flush method.

更新3:我注意到这种情况,如果我在一个依赖注入或者$位置或$途径(间接的,它与我到处使用一些基本的服务情况)

Update 3: I noticed this happens if I inject either $location or $route in one of the dependencies (indirect ones, it happens with some basic service that I use everywhere).

更新4:这里是一个plunker重现吧! http://plnkr.co/edit/7XweXI?p=info

Update 4: Here is a plunker to reproduce it! http://plnkr.co/edit/7XweXI?p=info

推荐答案

范围的事件这是$广播()'d或$放出()倒是是同步的,当他们完成后返回的事件对象。

Scope events which are $broadcast()'d or $emit()'d are synchronous, and return the event object when they're finished.

在角有些事情取决于返回的事件对象,如ngRoute上。

Some things in Angular depend on the returned event object, such as ngRoute.

所以,你能做些什么来解决这个问题是简单地添加 andCallThrough()当您创建的间谍,所以它调用原始功能,并产生一个返回值

So what you can do to fix this is simply add andCallThrough() when you create your spy, so that it calls the original function and yields a return value.

另外,如果你不想使用真正的$广播,你可以使用 andCallFak​​e()来提供一个假的函数,可以返回任何你想要的。

Alternatively, if you don't want to use the real $broadcast, you can use andCallFake() to supply a fake function which can return anything you want.

例如:

beforeEach(function(){
  module('MyApp');
  inject(function($injector){
    rootScope = $injector.get('$rootScope');
    spyOn(rootScope,'$broadcast').andCallThrough(); // will prevent the reference error
  })
});

这篇关于类型错误:未定义无法读取属性“默认prevented”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆