Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – $控制器服务在加载控制器时抛出错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用角度1.5.8.这是我的代码
describe('My Controller',function() {
    var MyController;
    var $controller;
    var $rootScope;
    var $state;

    beforeEach(angular.mock.module('ui.router'));
    beforeEach(module('app.my.ctrl'));
    beforeEach(inject(function(_$controller_,_$rootScope_,_$state_) {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $state = _$state_;
        MyController = $controller('MyController',{ scope: $rootScope.$new() });
    }));

    describe('#init',function() {
        it('should do something',function() {
            console.log('logStatement',MyController);

            MyController.init();

            expect(true).toBe(true);
        })

    })
});

测试运行器能够找到所有文件,所以这不是一个忘记加载的东西的情况.当我运行这个测试,不仅logStatement不会出现,我得到这个错误

Argument 'MyController' is not a function,got undefined

这是我的控制器

(function() {
'use Strict';

angular
    .module('app.my.ctrl')
    .controller('MyController',MyController);

MyController.$inject = [
    '$scope'
];
/* ngInject */
function MyController($scopE) {

    var vm = this;

    vm.Hello = 'world';

    vm.init = function() {
        return true;
    }
}

})();

这是我的业务conf文件

// Karma configuration

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files,excludE)
    basePath: '',// frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],// list of files / patterns to load in the browser
    files: [
      'bower_components/angular/angular.js','bower_components/angular-mocks/angular-mocks.js','bower_components/angular-ui-router/release/angular-ui-router.js','src/controllers/MyController.js','tests/unit/**/*.spec.js',],// list of files to exclude
    exclude: [
      '**/*.swp'
    ],// preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },// test results reporter to use
    // possible values: 'dots','progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['spec'],//  Spec Reporter Config
    specReporter: {
    //     suppressErrorSumMary: false,//     suppressFailed: false,//     suppressPassed: false,suppressSkipped: true
    //     showSpectiming: false
    },// web server port
    port: 9876,// enable / disable colors in the output (reporters and logs)
    colors: true,// level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,// enable / disable watching file and execuTing tests whenever any file changes
    autoWatch: true,// start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],// ConTinuous Integration mode
    // if true,Karma captures browsers,runs the tests and exits
    singleRun: true,// Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
};

这是什么意思??我在文档中找不到任何解释这一点的东西.

更新:

我已经读了this answer,答案没有奏效.

尝试将控制器中注入的服务从范围更改为范围
beforeEach(inject(function(_$controller_,_$state_) {
      $controller = _$controller_;
      $rootScope = _$rootScope_;
      $state = _$state_;
      MyController = $controller('MyController',{ $scope: $rootScope.$new()});
}));

大佬总结

以上是大佬教程为你收集整理的angularjs – $控制器服务在加载控制器时抛出错误全部内容,希望文章能够帮你解决angularjs – $控制器服务在加载控制器时抛出错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。