私は、リクエストが実行されているときに(学習目的で)gifのロードを設定するアプリ機能を書いています。私は AngularJS 1.4.5 の「Controller as」構文と John Papa スタイル ガイドを使用しています。そこで、現在のリクエストの量をサービスに渡すインターセプターを作成しました。
(function () {
'use strict';
angular
.module('eventApp')
.factory('requestInterceptorService', requestInterceptorService);
requestInterceptorService.$inject = ['$q', 'loadingService'];
function requestInterceptorService($q, loadingService) {
var numLoadings = 0;
var requestInterceptorServiceFactory = {
request: request,
response: response,
responseError: responseError
};
return requestInterceptorServiceFactory;
function request(config) {
numLoadings++;
loadingService.setLoaderStatus(numLoadings);
return config || $q.when(config);
}
function response(response) {
numLoadings--;
loadingService.setLoaderStatus(numLoadings);
return response || $q.when(response);
}
function responseError(response) {
numLoadings--;
loadingService.setLoaderStatus(numLoadings);
return $q.reject(response);
}
}
})();
これは、ロード中の画像を表示する必要があるかどうかを示すフラグisLoadgerEnabledを使用した私のloading.serviceです。
(function () {
'use strict';
angular
.module('eventApp')
.factory('loadingService', loadingService);
function loadingService() {
var isLoaderEnabled = false;
var loadingServiceFactory = {
setLoaderStatus: setLoaderStatus,
getLoaderStatus: getLoaderStatus,
isLoaderEnabled: isLoaderEnabled
};
return loadingServiceFactory;
function setLoaderStatus(numberOfRequests) {
var status = false;
if (numberOfRequests === 0) {
status = false;
}
if (numberOfRequests !== 0) {
status = true;
}
isLoaderEnabled = status;
}
function getLoaderStatus() {
return isLoaderEnabled;
}
}
})();
上記のコードは私にとってはうまくいきます。ビューには、インデックスコントローラーからのフラグをリッスンする、ロードイメージとng-show ディレクティブを含むdivがあります。
<div id="loaderDiv">
<img src="client/assets/img/loader.gif" class="content-loader"
ng-show="index.isLoaderEnabled" />
</div>
.controller('indexController', indexController);
indexController.$inject = ['$location', 'authService', 'authModal', 'loadingService', '$scope'];
function indexController($location, authService, authModal, loadingService, $scope) {
var vm = this;
vm.isLoaderEnabled = loadingService.isLoaderEnabled;
//code with other functionality
$scope.$watch(function () {
return loadingService.isLoaderEnabled;
}, function (newValue) {
vm.isLoaderEnabled = newValue;
});
}
})();
私の問題: vm.isLoaderEnabledはサービスで更新されず(実際には vm.isLoaderEnabled は常に false)、どこに問題があるのか わかりません。この機能のための効率的で洗練されたソリューションを、おそらく $scope なしで (可能であれば) 書きたいと思います。質問、リファクタリングのアドバイス、またはデータをバインドして表示するためのより良いアイデアをお待ちしています。