Angularjs - Decorate Controllers

后端 未结 2 358
你的背包
你的背包 2021-01-05 17:42

I am trying to set up a decorator for my controllers. My intention is to introduce some common behaviour across all the controllers in my app.

I have it configured

2条回答
  •  孤独总比滥情好
    2021-01-05 18:03

    In Angular 1.4.x modules have decorator method, $provide.decorator is no longer needed.

    For monkey-patching APIs it is always preferable to use arguments instead of enumerating them explicitly, the chance that it will break is much lesser.

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

提交回复
热议问题