【问题标题】:$provide.decorator $controller return throw undefined is not a function angularjs$provide.decorator $controller return throw undefined is not a function angularjs
【发布时间】:2015-05-01 17:56:56
【问题描述】:

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

这是我的文件树:

  • index.html
  • app.js
  • 浏览量
    • prod.html
    • cat.html
  • 控制器
    • prod.js
    • cat.js

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

Index.html

<body data-ng-app="myApp">
  <div data-ng-view=""></div>  
</body>

Default.js 使用 AngularJS v1.3.14

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

<div class="row" data-ng-controller="prodCtrl">
{{test}}  
</div>

Prod.js

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

问题是错误:“undefined is not a function”。 (参见 Default.js 中的代码)
如果问题不清楚,我很乐意解释更多。

【问题讨论】:

  • 你能展示你提供 loadControllerScript 函数的代码吗?
  • 不相关。 “$provide.decorator('$controller', ['$delegate', function ($delegate) {” - 在“loadControllerScript”之前抛出的异常。我会更新我的问题。

标签: javascript jquery html angularjs


【解决方案1】:

您不能在配置阶段请求实例(例如 $controller)。您只能访问那里的提供商。查看docs

配置块 - 在提供者注册和配置阶段执行。只有提供者和常量 可以注入到配置块中。这是为了防止 在服务完全完成之前意外实例化服务 配置好了。

更新:这可行:

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);
        }
    }])
}]);

检查这个demo

【讨论】:

  • 那么,我在哪里可以运行将动态加载控制器脚本的代码?
  • 谢谢!它正在工作。现在,我在第 7698 行(未压缩版本)“forEach(控制器,函数(控制器){”我正在使用角度 v1.3.14 的角度代码脚本中收到另一个错误“对象不是函数”。
  • 似乎存在兼容性问题。该代码仅适用于 Angular 1.2.x 或更低版本。
  • 对于 Angular 1.3.x 及更高版本,将委托 fn 返回为 return $delegate(constructor, locals, true);。见stackoverflow.com/questions/32442605/…
猜你喜欢
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2015-06-13
  • 2014-08-15
相关资源
最近更新 更多