Angularjsサービス内の変数の値を、同じモジュール内のコントローラから変更できるようにする必要があります。 TL、DRバージョンは下にあります...Angularjsサービス内の変数の値をコントローラからどのように動的に変更するのですか?
私は、スワッガーファイルで記述されたRESTful APIを備えた組み込みシステムを持っています。私はAngularjsアプリ経由でアクセスしています。このアプリケーションを作成するために、swagger-codegenを使用してサービスを自動生成しました。この結果、$ httpへの呼び出しからサービスの内部で使用されるベースパスが渡されます。
var myApp = angular.module('MyApp', [
])
.service('MyApi', API.Client.MyApi)
.value('MyApiBasePath', 'http://192.168.1.128');
これをすべてがうまく動作します、私は次のようしている私の角度app.jsファイルで
API.Client.MyApi = function($http, $httpParamSerializer, $injector) {
/** @private {!string} */
this.basePath_ = $injector.has('MyApiBasePath') ?
/** @type {!string} */ ($injector.get('MyApiBasePath')) :
'http://localhost/';
...
}
:サービスは次のようになります。しかし、アプリケーション内からデバイスのベースパス(具体的にはIPアドレス)を設定できるようにしたい。しかし、サービスは当初から開始されており、コントローラにサービス変数basePath_を更新する方法を知らせていません。
私はパラメータとしてベースパスを受け入れるようにサービス関数を書き直すことができましたが、swagger-codegenによって自動生成されたサービスを書き直したくありません。
私はすべての解決策に開放されています。これを行うには全体的に最善の方法かもしれないとアドバイスしています。
UPDATE:5/24/2016 - 誰かがぼんやりしたコードを投稿するように頼んだ。ここには、初期サービス機能とメソッドを含むファイルの1つの関連部分があります。
API.Client.MyApi = function($http, $httpParamSerializer, $injector) {
/** @private {!string} */
this.basePath_ = $injector.has('MyApiBasePath') ?
/** @type {!string} */ ($injector.get('MyApiBasePath')) :
'http://localhost/';
/** @private {!Object<string, string>} */
this.defaultHeaders_ = $injector.has('MyApiDefaultHeaders') ?
/** @type {!Object<string, string>} */ (
$injector.get('MyApiDefaultHeaders')) :
{};
/** @private {!angular.$http} */
this.http_ = $http;
/** @private {!Object} */
this.httpParamSerializer_ = $injector.get('$httpParamSerializer');
}
API.Client.MyApi.$inject = ['$http', '$httpParamSerializer', '$injector'];
/**
* Thingy Outputs
* Returns the state of all thingys
* @param {!angular.$http.Config=} opt_extraHttpRequestParams Extra HTTP parameters to send.
* @return {!angular.$q.Promise<!Array<!API.Client.boolGetModel>>}
*/
API.Client.MyApi.prototype.thingyGet = function(opt_extraHttpRequestParams) {
/** @const {string} */
var path = this.basePath_ + '/thingys';
/** @type {!Object} */
var queryParameters = {};
/** @type {!Object} */
var headerParams = angular.extend({}, this.defaultHeaders);
/** @type {!Object} */
var httpRequestParams = {
method: 'GET',
url: path,
json: true,
params: queryParameters,
headers: headerParams
};
if (opt_extraHttpRequestParams) {
httpRequestParams = angular.extend(httpRequestParams, opt_extraHttpRequestParams);
}
return this.http_(httpRequestParams);
}
「を規定"メソッド(上記のswaggerの例で)私はあなたがお勧めするものを試みるときに--service/.valueを角モジュールの宣言の一部として使用します(var myserviceobj =新しいAPI.Client.MyApiの場合は " API .Client.MyApiはコンストラクタではありません。 " – user5890478
swagger-codegenによって生成されたすべてのファイルをここに投稿できますか? –
上記のコードを編集してswagger-codegenファイルを表示しました。 – user5890478