2014-09-05 76 views
3

我的代码: 的index.html角+ require.js:无法实例化模块

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

我有问题:

Uncaught Error: [$injector:modulerr] Failed to instantiate module Arrows due to: Error: [$injector:nomod] Module 'Arrows' 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.

回答

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. 
+0

我添加了angular.module(“箭头”,[]),但结果相同....请帮助我..... ..... – 2014-09-05 07:13:39

+0

添加angular.module(“Arrows”,[] )。 http://plnkr.co/edit/ysdNKDXcHB1BBgXwQcIf?p=preview也许你的问题是使用require,backbone等你使用Angular时真的需要这些框架吗? – knutesten 2014-09-05 10:03:14

8

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

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

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

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

+0

我得到错误:错误:[ng:areq]参数'我'不是一个函数,得到undefined – 2014-09-05 07:16:45

+0

你可以准备一个jsfiddle或plunker作为游乐场吗?因此,我们可以编辑并查看出了什么问题。 – turhanco 2014-09-05 07:23:15

+0

感谢队友,这为我解决了它。 – Fabii 2015-03-13 02:28:11

0

从我的理解,这是加载顺序的问题,我可能是错了,但根据我所经历:

  • 的HTML文件加载第一
  • requireJs文件执行

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

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

对我来说,这不是一个解决方案,只是一个解决方法,我相信requireJs可以(或应该)做得更好,所以无论是requireJs的bug还是另一个解决方案。

希望这会有所帮助!