cari

Rumah  >  Soal Jawab  >  teks badan

angular.js - 用requireJS模块angularjs代码时遇到一些问题

原本的angularjs项目是可用的,但是在用requireJS时出错了。
出错的是app.js
原本的angularjs代码中的app.js代码是

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: '/'
            });
    });

用了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;
    })
})()

浏览器报错注入出错了。。。接触requirejs不久,有没有大神教教该怎么改。

阿神阿神2744 hari yang lalu499

membalas semua(2)saya akan balas

  • 習慣沉默

    習慣沉默2017-05-15 17:00:25

    Masalahnya jelas di sini:

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

    Kebergantungan anda masih harus ditulis sebagai ['ngRoute', 'ngResource'].

    balas
    0
  • 怪我咯

    怪我咯2017-05-15 17:00:25

    Saya tidak faham, ng sudah melakukan DI, mengapa kita perlu menggunakan pemuat lain?

    balas
    0
  • Batalbalas