$ provide.decorator $ controller返回throw undefined不是一个函数angularjs

我想通过视图加载控制器时添加动态控制器的脚本。

这是我的文件树:

  • 的index.html
  • app.js
  • 意见
    • prod.html
    • cat.html
  • 控制器
    • prod.js
    • cat.js

我希望当用户获得/prod/ URL时,应用程序将动态地加载(在视图中)prod.html和prod.js用于控制器的逻辑。
所有这些逻辑当然都需要ng-route。

的index.html

  

使用AngularJS v1.3.14的Default.js

 var app = angular.module('myApp', ['ngRoute']); app.config(function ($routeProvider, $locationProvider, $controllerProvider, $provide) { $locationProvider.html5Mode({ enabled: true, requireBase: false }); app.registerCtrl = $controllerProvider.register; $routeProvider. when('/:name', { templateUrl: function (urlAttr) { return '/views/' + urlAttr.name + '.html'; } }); // This code throw error: //TypeError: undefined is not a function //at angular.min.js:1 //at r (angular.min.js:1) //at at (angular.min.js:1) //at b (angular.min.js:1) //at b (angular.min.js:1) //at b (angular.min.js:1) //at b (angular.min.js:1) //at $get.t (angular.min.js:1) //at angular.min.js:1 //at p.$get.p.$eval (angular.min.js:1) $provide.decorator('$controller', ['$delegate', function ($delegate) { // I want to get the controller name and than load the js file. // return function (constructor, locals) { // if (typeof constructor == "string") { // locals.$scope.loadControllerScript(constructor); // } // return $delegate(constructor, locals); // } }]); }); 

Prod.html

 
{{test}}

Prod.js

 app.registerCtrl('prodCtrl', function ($scope) { $scope.test = ''; }); 

问题是错误: “undefined不是函数” 。 (参见Default.js中的代码)
如果问题不明确,我将很乐意解释更多。

在配置阶段,您不能要求实例(例如$ controller)。 您只能访问那里的提供商。 检查文档 :

配置块 – 在提供程序注册和配置阶段执行。 只有提供程序和常量才能注入配置块。 这是为了防止在完全配置服务之前意外实例化服务。

更新:这有效:

 var app = angular.module("myApp", ['ng'], ['$provide', function($provide) { $provide.decorator('$controller', ['$delegate', function($delegate) { return function(constructor, locals) { console.log("executing custom code ..."); return $delegate(constructor, locals); } }]) }]); 

查看此演示