angular + require.js:无法实例化模块

时间:2014-09-05 05:43:05

标签: angularjs requirejs

我的代码: 的的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]模块&#39;箭头&#39;不是   可以!您要么错误拼写了模块名称,要么忘记加载它。   如果注册模块,请确保将依赖项指定为   第二个论点。

3 个答案:

答案 0 :(得分:8)

从html中删除ng-app指令。你已经手动引导它了。

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

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

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

答案 1 :(得分:1)

您必须实例化模块

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.

答案 2 :(得分:0)

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

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

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

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

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

希望这有帮助!