2014-01-10 74 views
2

我正在使用requirejs和angularjs在我的应用程序中加载模块。 我面临的问题是我不知道如何加载角度已被引导后的控制器。这里是我的代码:引导后的加载控制器angularjs

main.js:

require(['app', //all my scripts go here 
function(){ 
angular.bootstrap(document, ['myApp']); 
}); 

app.js:

define([], function(){ 
    var myApp = angular.module('myApp', ['ngRoute']); 
    $routeProvider. 
     when('/home', { 
       templateUrl : 'partials/home.html', 
       controller : 'UserRegisterCtrl' 
      }). 
      otherwise({ 
       redirectTo : '/home' 
      }) 
    return myApp; 
}) 

controller.js

define(['app'], function(app){ 
    app.controller('MyCtrl', function($scope){}); 
}) 

myApp.html

body(ng-app) 
    <div ng-controller = 'MyCtrl'></div> <-- I can not put MyCtrl here 
because I think the MyCtrl has been declared before myApp has been bootstrapped 

因此,我真的需要知道是否有一种方法可以在引导myApp后加载MyCtrl控制器。我对Angularjs非常陌生。任何帮助将非常感激。

+0

一个复杂的问题。看看几个努力,还有更多的野外......没有完成我认为:[angular-require-lazy](https://github.com/nikospara/require-lazy),[angularAMD ](https://github.com/marcoslin/angularAMD) –

回答

1

当您在HTML中使用ng-app,引导自动发生。在这种情况下,这取决于您在index.html中放入<script>标签的内容。

我假设你按照RequireJS的用法使用RequireJS加载angular。如果是这样,请确保您使用shim声明angularapp的依赖关系。如果情况并非如此,请说明您的装载angular的方式,并分享您的main.js的内容。

这真是棘手,我让RequireJS与AngularJS工作,所以我创建了以下项目,帮助我处理复杂:

http://marcoslin.github.io/angularAMD