【问题标题】:angular + require.js :Failed to instantiate moduleangular + require.js:无法实例化模块
【发布时间】:2014-09-05 05:43:05
【问题描述】:

我的代码: index.html

<html ng-app='Arrows'>
 <script data-main="main" src="require.js"></script>
<div ng-controller="My">
  {{status}}
</div>

还有main.js文件

    require(["jquery","underscore","backbone",
"marionette","angular"], function($ ,_,Backbone,Marionette,angular){

      debugger

       var app = angular.module("Arrows")
      angular.element(document).ready(function () {
            angular.bootstrap(document, ['Arrows']);
        });  

      app.controller('My', function($scope) {
            $scope.status = 'hello! Its working!';
        });
    })

我有问题:

未捕获的错误:[$injector:modulerr] 无法实例化模块 由于以下原因导致的箭头:错误:[$injector:nomod] 模块“箭头”不是 可用的!您要么拼错了模块名称,要么忘记加载它。 如果注册模块,请确保将依赖项指定为 第二个参数。

【问题讨论】:

    标签: angularjs requirejs


    【解决方案1】:

    从你的 html 中移除 ng-app 指令。您已经手动引导它。

    <script data-main="main" src="require.js"></script>
    <div ng-controller="My">
      {{status}}
    </div>
    

    这对于代码来说已经足够了。

    顺便说一句,我不会在同一个应用程序中使用 Backbone 和 Angular。

    【讨论】:

    • 我收到错误:错误:[ng:areq] 参数 'My' 不是函数,未定义
    • 你能准备一个 jsfiddle 或 plunker 作为游乐场吗?因此我们可以编辑并查看问题所在。
    【解决方案2】:

    你必须实例化模块

    angular.module('name', [])  // this will instantiate the module
    
    angular.module('name')      // this will not instantiate the module and give you 
                                // an error if the previous line has not been called first. 
                                // This function can be used to add controllers, services 
                                // etc. to the module, for example in another file.
    

    【讨论】:

    • 我添加了 angular.module("Arrows",[]) 但结果相同....请帮帮我.....请
    • 我在添加 angular.module("Arrows", []) 时可以正常工作。 plnkr.co/edit/ysdNKDXcHB1BBgXwQcIf?p=preview 可能你的问题是使用 require、backbone 等。在使用 Angular 时你真的需要这些框架吗?
    【解决方案3】:

    根据我的理解,这是加载顺序的问题,我可能会弄错,但根据我的经验:

    • html 文件首先加载
    • requireJs 文件被执行

    然后,问题是每次使用角度指令时,它都会在实际模块/控制器/等之前解决。已加载。

    将应用程序引导到文档的技巧有效,但为了拥有一个功能齐全的应用程序,您需要手动引导所有内容,而不是使用“ng-controller”(和其他指令) )。

    对我来说,这不是一个解决方案,只是一种解决方法,我确信 requireJs 可以(或应该)做得更好,所以要么是 requireJs 的错误,要么有其他解决方案。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2014-06-04
      相关资源
      最近更新 更多