首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >窗口:‘TypeError’不是函数(计算'angular.element(window).width()')

窗口:‘TypeError’不是函数(计算'angular.element(window).width()')
EN

Stack Overflow用户
提问于 2014-07-23 07:02:06
回答 2查看 6.2K关注 0票数 0

嗨,我有一个非常复杂的应用程序,我一直在为它编写Karma单元测试。我有很多测试都写得很成功,运行也很成功,但后来我在应用程序中更改了一些东西,现在我收到了大量的错误。我盯着它看了好几个小时都搞不明白。

我收到以下错误:

代码语言:javascript
复制
TypeError: 'undefined' is not a function (evaluating 'angular.element(window).width()')
    at /Users/Desktop/app/scripts/app.js:9
    at invoke (/Users/Desktop/app/bower_components/angular/angular.js:3869)
    at /Users/Desktop/app/bower_components/angular/angular.js:3715

我的配置文件如下所示

代码语言:javascript
复制
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html

module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '',

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/bower_components/angular-resource/angular-resource.js',
  'app/bower_components/angular-cookies/angular-cookies.js',
  'app/bower_components/angular-sanitize/angular-sanitize.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/bower_components/angular-bootstrap/*.js',
  'app/bower_components/angular-ui-date/src/date.js',
  'app/bower_components/angular-ui-sortable/*.js',
  'app/bower_components/angular-ui-router/src/*.js',
  'app/bower_components/d3/*.js',
  'app/bower_components/nvd3/*.js',
  'app/bower_components/angularjs-nvd3-directives/src/directives/*.js',
  'app/bower_components/jquery/dist/*.js',
  'app/bower_components/ng-grid/*.js',
  'app/bower_components/ng-grid/build/*.js',
  'app/bower_components/ng-grid/plugins/*.js',
  'app/scripts/app.js',
  'app/bower_components/angular',
  'app/scripts/*.js',
  'app/scripts/**/*.js',
  'app/scripts/***/**/*.js',
  'test/client/spec/**/*.js'
    ],

// list of files / patterns to exclude
exclude: [
'app/bower_components/*/angular-scenario.js', 
'app/bower_components/angular-ui-router/src/compat.js',
'app/bower_components/angularjs-nvd3-directives/src/directives/intro.js',
'app/bower_components/angularjs-nvd3-directives/src/directives/outro.js'
],

// web server port
port: 8080,

// level of logging
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// Start this browser
browsers: ['PhantomJS'],

//Coverage Options
preprocessors: {
  'app/scripts/**.js': 'coverage'
},
reporters: ['dots', 'coverage']

});
};

我正在测试的控制器:

代码语言:javascript
复制
angular.module("vizApp").controller("BasicSearchCtrl", function ($rootScope, $scope, SearchService) {
    "use strict";

    $scope.searchContent = null;

    $scope.$watch("searchContent", function (newVal, oldVal, scope) {
    SearchService.setBasicSearch($scope.searchContent);
    });

});

我要写的测试是:

代码语言:javascript
复制
describe("Controller: BasicSearchCtrl", function () {
    "use strict";
    var scope, BasicSearchController, httpBackend, searchSerivce;

    //load the controller"s module
    beforeEach(module("vizApp"));

    beforeEach(function () {
        angular.mock.inject(function ($injector) {
        httpBackend = $injector.get("$httpBackend");
        });
    });

//initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $injector) {

    // create a new child scope
    scope = $rootScope.$new();

    scope.SearchService = $injector.get("SearchService");

    //create a new instance of basic search controller
    BasicSearchController = $controller("BasicSearchCtrl", { $scope: scope });

}));

//check the initial state of the search content
it("very basic search", function () {

    expect(scope.searchContent).toBeUndefined;
    expect(scope.SearchService.basicSearch).toBeUndefined;

});

但是我一直在app.js上得到这个神秘的error...about代码行9,但是app.js看起来像这样,所以我不明白。

代码语言:javascript
复制
angular.module('vizApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ui.bootstrap',
'ui.router',
'nvd3ChartDirectives',
'ngGrid',
'ui.sortable',
'ui.date'
]).config(function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {

'use strict';

// For any unmatched url, redirect to /
$urlRouterProvider.otherwise('/');
EN

回答 2

Stack Overflow用户

发布于 2014-07-31 13:25:12

width()不受angular支持,angular在内部依赖于jqLite https://docs.angularjs.org/api/ng/function/angular.element

您必须包含jquery并使用jquery的width()函数,或者更好的做法是注入Angular的$window服务并使用$window.innerWidth、$window.offsetWidth或$window.clientWidth。

票数 1
EN

Stack Overflow用户

发布于 2014-09-01 19:35:50

如果你真的想使用jquery而不是jqlite,那么你的模拟配置就是问题所在:

只需将jquery.js移动到文件列表中的第一个位置。如果jquery是在angular.js之后,你只会得到一个没有视窗方法的jqlite对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24899282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档