6

コントローラー用のデコレーターをセットアップしようとしています。私の意図は、アプリ内のすべてのコントローラーにいくつかの共通の動作を導入することです。

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/ (壊れた)

4

2 に答える 2

2

私自身の質問に答えます。

何が起こっているのかを理解するためにAngularのソースコードを掘り下げなければなりませんでした。

$controller インスタンスは、以下のコードを使用して作成されます。修正はパラメータ ' later ' にあります。これはtrueに設定する必要があります。

    return function(expression, locals, later, ident) {
      // PRIVATE API:
      //   param `later` --- indicates that the controller's constructor is invoked at a later time.
      //                     If true, $controller will allocate the object with the correct
      //                     prototype chain, but will not invoke the controller until a returned
      //                     callback is invoked.

上記の引用: https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js

更新されたプロバイダー コード:

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

 return function(constructor, locals) {

            //Custom behaviour code

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

更新されたフィドル: http://jsfiddle.net/v3067u98/1/

于 2015-09-07T16:43:34.047 に答える