我正在尝试为我的控制器设置一个装饰器.我的目的是介绍我的应用程序中所有控制器的一些常见行为.
我已经将它配置为在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);
};
});