angular js动态加载模块+ requirejs

时间:2015-02-15 11:36:46

标签: angularjs requirejs jasmine karma-runner karma-jasmine

 controller.js

    define([
      'angular',
      'app',
      'underscore',
      'jquery'
    ],
     function (angular, app, _, $) {
      'use strict';

  var module = angular.module('kibana.panels.dummy', []);
  app.useModule(module);

  module.controller('dummy', function($scope, dashboard, querySrv, filterSrv) {
    $scope.heading = 'Welcome';
    $scope.panelMeta = {
      status : "Beta",
      description : "Dummy module for demonstartion"
    };
.......


controllerSpec.js

'use strict';

define(['./controller'], function(app)
{
    describe("The 'dummy'", function()
    {
        var $rootScope;
        var $controller;
        var $scope;

        beforeEach(function()
        {

            module('kibana');
            module('kibana.panels.dummy');


            inject
            ([
                '$injector',
                '$rootScope',
                '$controller',

                function($injector, _$rootScope, _$controller)
                {
                    $rootScope = _$rootScope;
                    $scope = $rootScope.$new();
                    console.log(_$controller)
                    $controller = _$controller;
                }
            ]);

            $controller('dummy', {$scope: $scope});

        });

        it("should set the page heading to 'Welcome'", function()
        {
            expect($scope.page).toBe('Welcome');
        });
    });
});
........................


app.js
 // bootstrap the app
    angular
      .element(document)
      .ready(function() {
        $('body').attr('ng-controller', 'DashCtrl');
        angular.bootstrap(document, apps_deps)
          .invoke(['$rootScope', function ($rootScope) {
            _.each(pre_boot_modules, function (module) {
              console.log("extend ... ", module.name)
              _.extend(module, register_fns);
            });
            pre_boot_modules = false;

            $rootScope.requireContext = appLevelRequire;
            $rootScope.require = function (deps, fn) {
              var $scope = this;
              $scope.requireContext(deps, function () {
                var deps = _.toArray(arguments);
                $scope.$apply(function () {
                  fn.apply($scope, deps);
                });
              });
            };
          }]);
      });
.................


when run karma start

错误:来自kibana.panels.dummy的未知提供者:$ controllerProvider         在错误(本机)         at /home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:2832:42         在Object.getService [as get](/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:2960:39)         at /home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:2912:54         at Array.forEach(native)         在forEach(/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:130:11)         在loadModules(/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:2900:5)         在Object.createInjector [作为注入器](/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/vendor/angular/angular.js:2842:11)         at Object.workFn(/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/bower_components/angular-mocks/angular-mocks.js:2339:52)         在window.inject.angular.mock.inject(/home/badrit004/Projects/solr-4.8.1/SiLK/banana-webapp/webapp/src/bower_components/angular-mocks/angular-mocks.js:2322:37 < / p>

1 个答案:

答案 0 :(得分:-2)

没有查看问题的详细信息,但对于延迟加载模块,我们使用ocLazyLoad:https://github.com/ocombe/ocLazyLoad 它对我们很有用。我们将requirejs与loader一起使用。