angular.js - Encountering some problems when using requireJS module angularjs code
阿神
阿神 2017-05-15 16:58:25
0
2
524

The original angularjs project is available, but an error occurred when using requireJS.
The error is app.js
The app.js code in the original angularjs code is

angular.module('todomvc', ['ngRoute', 'ngResource'])
    .config(function ($routeProvider) {
        'use strict';

        var routeConfig = {
            controller: 'TodoCtrl',
            templateUrl: 'todomvc-index.html',
            resolve: {
                store: function (todoStorage) {
                    // Get the correct module (API or localStorage).
                    return todoStorage.then(function (module) {
                        module.get(); // Fetch the todo records in the background.
                        return module;
                    });
                }
            }
        };

        $routeProvider
            .when('/', routeConfig)
            .when('/:status', routeConfig)
            .otherwise({
                redirectTo: '/'
            });
    });

After using requirejs
main.js

(function  () {
    require.config({
        paths: {
            'angular': '../node_modules/angular/angular',
            'angular-route': '../node_modules/angular-route/angular-route',
            'angular-resource': '../node_modules/angular-resource/angular-resource'
        },
        shim: {
            'angular': {
                exports: 'angular'
            },
            'angular-route': {
                deps: ['angular'],
                exports: 'angular-route'
            },
            'angular-resource': {
                deps: ['angular'],
                exports: 'angular-resource'
            }
        },
        deps: ['bootstrap']
    })
})()

app.js

(function  () {
    define(['angular','angular-route','angular-resource'],function (angular){
        var moduleName = 'myAppModule';
        angular.module(moduleName, ['angular-route','angular-resource'])
    .config(function ($routeProvider) {
        'use strict';

        var routeConfig = {
            controller: 'TodoCtrl',
            templateUrl: 'todomvc-index.html',
            resolve: {
                store: function (todoStorage) {
                    // Get the correct module (API or localStorage).
                    return todoStorage.then(function (module) {
                        module.get(); // Fetch the todo records in the background.
                        return module;
                    });
                }
            }
        };

        $routeProvider
            .when('/', routeConfig)
            .when('/:status', routeConfig)
            .otherwise({
                redirectTo: '/'
            });
    });
    return moduleName;
    })
})()

The browser reported an error with the injection. . . I have been in contact with requirejs for a while. Is there anyone who can teach me how to change it?

阿神
阿神

闭关修行中......

reply all(2)
習慣沉默

The problem is obviously here:

angular.module(moduleName, ['angular-route','angular-resource'])

Your dependencies should still be written['ngRoute', 'ngResource'].

刘奇

I don’t understand, since ng has already done DI, why do we need to use another loader?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template