javascript - RequireJS + Angular : Undefined app. 回调不触发

标签 javascript angularjs module requirejs amd

我用这个简单的代码得到了 main.js:

'use strickt';

require.config({

  paths: {
    'angular'        : 'libs/angular'          ,
    'angular-router' : 'libs/angular-route'    ,
  },
  shim : {
    'angular' : {
      exports : 'angular'
    },
    'angular-router' : {
      deps: ['angular']
    }
  }

});

require(['app'], function (mainApp) {
  console.log(mainApp);
});

如您所见,我尝试在 require 回调中获取应用程序。但我得到的都是未定义的。 这是我在 app.js 文件中得到的:

define('mainApp', ['angular', 'angular-route'], function (angular) {
  var app = angular.module('mainApp', ['ngRoute']);
  console.log('should be fired from app.js');
  return app;
});

那么问题来了: main.js 回调中未定义的函数参数“mainApp”似乎是合乎逻辑的,因为 app.js 中的 console.log() 不会射击。有人可以告诉我它有什么问题吗?

最佳答案

只需删除 app.js 文件中的模块名称(或将其更改为“app”)。您的 app.js 文件将如下所示:

define(['angular', 'angular-route'], function (angular) {
  var app = angular.module('mainApp', ['ngRoute']);
  console.log('should be fired from app.js');
  return app;
});

http://requirejs.org/docs/api.html#modulename

You can explicitly name modules yourself, but it makes the modules less portable -- if you move the file to another directory you will need to change the name. It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names. The optimization tool needs to add the names so that more than one module can be bundled in a file, to allow for faster loading in the browser.

关于javascript - RequireJS + Angular : Undefined app. 回调不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482394/

相关文章:

javascript - 通过指令的多个 AngularJS $http 请求正在阻止其他 JavaScript 方法

javascript - 检查数组中的所有值是否相等但忽略空值

javascript - GIF 的尺寸会影响网页的性能吗?

javascript - Angular JS 类型号在 Firefox 中不起作用

python - PyKEP python 开普勒工具箱

android - 使用位图工厂选项支持多个屏幕?

node.js - 当需要 Node.js 中的模块时, 'dot slash' (./) 是强制性的吗?

javascript - Contenteditable 在使用 foreColor 后重置文本颜色而不重置其他样式

javascript - 导入 Json 数据并在 Angular 应用程序中显示

javascript - 当 2 个数组更新时,angularjs 调用 watch