1

Angularjs と Typescript の両方を使用してコントローラーにファクトリを挿入しようとしましたが、このエラーが発生していますError: [$injector:unpr] http://errors.angularjs.org/1.2.9/$injector/unpr?p0=AuthenticationProvider%20%3C-%20Authentication

私は周りを研究してきましたが、私がやったことはいくつかの解決策に似ているため、解決策を見つけることができません。

これが私のログインコントローラーモジュールです

import Authentication = require('AuthenticationService');
module LoginController{

export interface UIMainScope extends ng.IScope {
    login: (credential: any) => void;
}

export class Controller {
    static $inject = ['$scope', '$http', 'Main', 'Authentication'];
    constructor (){}
}
export = LoginController
angular.module('App').controller('Controller', LoginController.Controller);

メソッドを呼び出すためにここに何かを注入するのを忘れましたか?

4

2 に答える 2

1

ここでの問題は、Angular$injectが Typescript から利益を得ることができないという事実に関連していrequireます。これらは独立した機能であり、異なる概念を表しています。

[$inject][1]組み込みのDependency Injectionとしての angularは、既に登録されているもののみを注入できます。Typescript オブジェクトへのアクセス権はありません。独自の (Angular の) Object Factory へのアクセスのみです。

エラーが言うように:不明なプロバイダー 'AuthenticationProvider'、私たちは呼び出す必要があります:

angular.module('App')
  .factory('Authentication', [... its dependencies ...
      , (params) => new Authentication(params)]);

これで、角度で認証の準備が整いました。コントローラー内でそれを要求できます。

構文は次のようになります。

// module with Scope and Controller
module LoginController
{
  export interface UIMainScope extends ng.IScope 
  {
    login: (credential: any) => void;
  }

  export class Controller
  {
    // constructor, instantiating private members this.$scope ...
    constructor($scope: UIMainScope
       , $http :ng.IHttpService
       , Main: any
       , Authentication: any)
    ...
  }
}

// and finally pass it into angular as .controller()
angular.module('App')
  .controller('Controller', [
     '$scope', '$http', 'Main', 'Authentication',
     ($scope, $http, Main, Authentication)  =>
     {
        return new LoginController.Controller($scope, $http, Main, Authentication);
     }
]);

最後に: 場合によっては、そのAuthenticationプロバイダーが Angular を使用して開始しても利益が得られない場合は、それを使用することもできます。登録する必要はまったくありません。require を呼び出して、そのメソッドにもアクセスするだけです...しかし、Angularの$injectプロセスをスキップする必要があります...

QueryStringBuilderこれはおそらくこのケースではありませんが、メソッドで渡されたパラメーターだけを消費する可能性のある...いくつか想像してみてくださいBuilder.build(...)

于 2014-07-03T16:26:36.137 に答える