【问题标题】:Updated to Angular 1.3.0 no longer working更新到 Angular 1.3.0 不再工作
【发布时间】:2014-12-14 15:37:19
【问题描述】:

我正在使用Browserify 来总结所有内容。我刚刚从1.2.23 升级到1.3.0,现在出现以下错误:

Firefox 错误

TypeError: angular.module is not a function

var app = angular.module('login-Controller', ['Views']);
   ----------^

明确表示没有定义角度。所以我从角度输出了输出

var angular = require('angular'),
console.info(angular); // Object {} ='s an empty object

这是否意味着 angular 不再与browserify 兼容?如果是,我怎样才能让它工作?

来自 Chrome 的详细错误

 Uncaught TypeError: undefined is not a function main.js:25868 C:\var\www\stage.mayfieldafc.com\src\site\js\users\loginCntrl.js.angularmain.js:1 smain.js:1 (anonymous function)main.js:28 ./src/site/js/app.js.angularmain.js:1 smain.js:1 emain.js:1 (anonymous function)
main.js:183 Uncaught Error: [$injector:modulerr] Failed to instantiate module mays due to:
Error: [$injector:nomod] Module 'mays' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.3.0/$injector/nomod?p0=mays
    at http://localhost:3000/js/main.js:183:12
    at http://localhost:3000/js/main.js:1900:17
    at ensure (http://localhost:3000/js/main.js:1824:38)
    at module (http://localhost:3000/js/main.js:1898:14)
    at http://localhost:3000/js/main.js:4167:22
    at forEach (http://localhost:3000/js/main.js:438:20)
    at loadModules (http://localhost:3000/js/main.js:4151:5)
    at createInjector (http://localhost:3000/js/main.js:4077:11)
    at doBootstrap (http://localhost:3000/js/main.js:1587:20)
    at bootstrap (http://localhost:3000/js/main.js:1608:12)
http://errors.angularjs.org/1.3.0/$injector/modulerr?p0=mays&p1=Error%3A%20…at%20bootstrap%20(http%3A%2F%2Flocalhost%3A3000%2Fjs%2Fmain.js%3A1608%3A12)main.js:183 (anonymous function)main.js:4190 (anonymous function)main.js:438 forEachmain.js:4151 loadModulesmain.js:4077 createInjectormain.js:1587 doBootstrapmain.js:1608 bootstrapmain.js:1502 angularInitmain.js:25682 (anonymous function)main.js:2845 triggermain.js:3116 eventHandler

标记

 html(ng-app='mays)

【问题讨论】:

  • 我在使用 Angular 1.3 和 Browserify 时遇到了同样的问题。 Angular 对象是空的,所以对angular.module 的调用是未定义的。似乎 npm 下载的 Angular 版本没有导出任何内容。有任何解决方案吗?
  • 是的,这就是问题所在。我将添加我如何修复它的答案。这是一个血腥可怕的可怕......可怕的hac KD:

标签: javascript angularjs browserify


【解决方案1】:

正如@JeffB 所提到的,您可以使用browserify-shim 来(希望暂时)解决这个问题。

首先,npm install --save-dev browserify-shim。然后,将以下内容添加到您的package.json

"browserify": {
    "transform": [
      "browserify-shim"
    ]
  },
  "browser": {
    "angular": "./node_modules/angular/angular.js"
  },
  "browserify-shim": {
    "angular": "angular"
  }

这应该让您require 并按预期访问 Angular。

【讨论】:

  • 这意味着你必须从包中删除它,否则你会得到名称冲突.. :/
  • @JamieHutber 我不确定你的意思?我现在在一个项目中工作,没有进一步的变化
  • 考虑到同样的错误,这对我有用。非常感谢!
【解决方案2】:

问题是 Angular 还没有更新他们的 npm 包以与 browserify 兼容。

我的修复很恶心,但它的工作。我只是下载了v1.3.0并粘贴到:

 node_modules\angular\lib\angular.min.js

现在 browserify 将编译并使用最新版本。我希望这是一个临时修复,直到他们更新软件包。

【讨论】:

  • 你不能用 browserify-shim 代替吗?这就是我对 1.3 所做的。
  • 我在填充时遇到了名称冲突。
【解决方案3】:

看起来您在脚本中将模块名称定义为“login-Controller”,但在标记中将其称为“可能”。我希望看到这样的东西:

//declare app in main.js
angular.module('name-of-app', [<modules>])

<!-- call your app in markup -->
<html ng-app="name-of-app"></html>

//jade
html(ng-app='name-of-app')

【讨论】:

  • 我不认为这是问题所在。这当然是 A 问题,但我认为这与 Browserify 要求后 Angular 对象为空这一事实无关。
最近更新 更多