在RequireJS r.js优化之后,AngularJs $ injector:modulerr

时间:2014-08-30 14:59:07

标签: javascript angularjs requirejs r.js

AngularJS-RequireJS应用程序构建正在抛出未捕获错误:[$ injector:modulerr]由于以下原因无法实例化模块myApp: 错误:[$ injector:unpr] r.js优化后出现未知提供程序:e 错误。这个问题之前并不存在。我在此link上跟踪了一个类似的问题,并在构建文件中添加了 mainConfigFile ,但问题仍然存在。以下是相应文件的代码

build.js

({
    mainConfigFile:'js/main.js',
    appDir: "./",
    baseUrl: "js",
    dir: "/home/karthic/optimized-test",
    modules: [
        {
            name: "main"
        }
    ]
})

main.js

require.config({
    baseUrl: './js',
    paths: {
        angular: 'libs/angular-1.2.9',
        angularRoute: 'libs/angular-route-1.2.9'
    },
    shim: {
        'angularRoute': {
            deps: ['angular'],
            exports: 'angularRoute'
        },
        'angular': {
            exports: 'angular'
        }
    }

});

require(['angular', 'controller', 'ang_when_routes', 'angularRoute'], function(angular, controller, angWhenRoutes, angularRoute) {
    'use strict';

    var app = angular.module('myApp', ['ngRoute']);
    angular.element(document).ready(function() {
        angular.bootstrap(document, ['myApp']);
    });

    angWhenRoutes.initRoutes(app);
    controller.controllerInit(app);

});

controller.js

define(function() {
     function controllerInit(app) {
         console.log('inside func cont');
         app.controller('mapApp', function($scope, $http, $location) {
             console.log('inside mapp,testing r.js optimizer');
         });
     }
     return {
         controllerInit: controllerInit
     }
 });

ang_when_routes.js

define(function() {
    function initRoutes(app) {
        app.config(
            function($routeProvider, $httpProvider) {
                $httpProvider.defaults.withCredentials = true;
                $routeProvider.
                when('/', {
                    templateUrl: 'index_content.html',
                    controller: 'mapApp'
                })

            });
    }
    return {
        initRoutes: initRoutes
    }
});

的index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Optimizer error</title>
        <script data-main="js/main.js" src="js/libs/require.js"></script>
</head>
<body>
    <div ng-view>

    </div>
</body>

</html>

index_content.html

<p>Testing the r optimizer</p>

如果错误可以修复,对我来说真的很有帮助

1 个答案:

答案 0 :(得分:1)

以下是@radimKohler在第一条评论中提出的答案。为了避免在缩小过程中出现angularJS问题内联数组注释必须按Angular Documentation中所述使用。以下是更改后修改后的代码

<强> controller.js

define( function () {
     function controllerInit(app) {
         console.log('inside func cont');
         app.controller('mapApp',['$scope','$http','$location', function ($scope, $http, $location) {
             console.log('inside mapp,testing r.js optimizer');

         }]);

     }
     return {
         controllerInit: controllerInit
     }

 });

<强> ang_when_routes.js

define(function(){
    function initRoutes(app){
        app.config(['$routeProvider','$httpProvider',
            function($routeProvider,$httpProvider){
                $httpProvider.defaults.withCredentials = true;
                $routeProvider.
                    when('/',{
                        templateUrl:'index_content.html',
                        controller:'mapApp'
                    })

            }]);    

    }
    return {
        initRoutes:initRoutes
    }
});