Angularjs – 装饰控制器

我正在尝试为我的控制器设置一个装饰器.我的目的是介绍我的应用程序中所有控制器的一些常见行为.

我已经将它配置为在Angular 1.2.x中工作,但是从1.3.x开始有一些重大更改,这些更改打破了代码.现在得到的错误是“控制器不是一个功能”.

下面是装饰器的代码

angular.module('myApp',['ng'],function($provide) {
    $provide.decorator('$controller',function($delegate) {

        return function(constructor,locals) {

                //Custom behavIoUr code

                return $delegate(constructor,locals);
            }
        })
    });

Angular 1.2.x – http://jsfiddle.net/3v17w364/2/(工作)
Angular 1.4.x – http://jsfiddle.net/tncquyxo/2/(破碎)

在Angular 1.4.x模块中有 decorator方法,不再需要$provide.decorator.

对于猴子修补API,总是最好使用参数而不是显式枚举它们,它会破坏的可能性要小得多.

angular.module('myApp',['ng']).decorator('$controller',function ($delegate) {
    return function (constructor,locals) {
        var controller = $delegate.apply(null,arguments);

        return angular.extend(function () {
            locals.$scope.common = ...;
            return controller();
        },controller);
    };
});

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...