AngularJS:当加载requirejs时,控制器未注册

AngularJS:当加载requirejs时,控制器未注册,angularjs,requirejs,Angularjs,Requirejs,我想通过requirejs动态加载一些控制器。我看到文件已加载,并且正确调用了模块.controller行。但是,控制器实际上没有在angular中注册,因此无法使用(我得到一个参数'lazyCtrl'不是函数,得到了未定义的异常) app.js var module = angular.module("myApp", []); module.controller("lazyCtrl", function() {}); module.controller("notLzayCtrl", fun

我想通过requirejs动态加载一些控制器。我看到文件已加载,并且正确调用了
模块.controller
行。但是,控制器实际上没有在angular中注册,因此无法使用(我得到一个
参数'lazyCtrl'不是函数,得到了未定义的
异常)

app.js

var module = angular.module("myApp", []);
module.controller("lazyCtrl", function() {});
module.controller("notLzayCtrl", function($scope) {
  $scope.loadController = function() {
    require(["lazy-controller.js"],function() {
      $scope.includedPath = "lazyControllerView.html"; // this is bound to an ng-view directive. It will try to uses the controller and eventually throw the "got undefined" exception
    });
  };
});
lazy controller.js

var module = angular.module("myApp", []);
module.controller("lazyCtrl", function() {});
module.controller("notLzayCtrl", function($scope) {
  $scope.loadController = function() {
    require(["lazy-controller.js"],function() {
      $scope.includedPath = "lazyControllerView.html"; // this is bound to an ng-view directive. It will try to uses the controller and eventually throw the "got undefined" exception
    });
  };
});
非懒惰控制器.js

var module = angular.module("myApp", []);
module.controller("lazyCtrl", function() {});
module.controller("notLzayCtrl", function($scope) {
  $scope.loadController = function() {
    require(["lazy-controller.js"],function() {
      $scope.includedPath = "lazyControllerView.html"; // this is bound to an ng-view directive. It will try to uses the controller and eventually throw the "got undefined" exception
    });
  };
});

有什么想法吗?谢谢

您必须声明控制器,但可以加载其内容。然而,这并不完全是“懒惰”

.controller('lazyCtrl', ['$scope', '$injector', function($scope, $injector) {
  require(['lazy-controller.js'], function(lazyCtrl) {              
    $injector.invoke(lazyCtrl, this, {'$scope': $scope});
  });
 }]);

你不能在angular应用程序已经引导(手动或通过ng应用程序自动)的点添加控制器哦,不!angular启动后是否可以添加指令?不,我不认为任何代码可以在启动后添加。这是可能的,只是需要技巧。请参阅: