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>


如果错误可以解决,那对我真的很有帮助

最佳答案

这是@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
    }
});

关于javascript - RequireJS r.js优化后的AngularJs $ injector:modulerr,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25583686/

10-12 06:37