あるサービスを使用して変数を保存し、service1 によって呼び出される Web API を呼び出す関数を保存する別のサービスを呼び出すことを考えていましたが、これは悪い考えでしょうか?
たとえば、Service1 からのみ値を取得するコントローラーがあります。
(function() {
'use strict';
angular
.module('app.event')
.controller('Controller1', Controller1);
Controller1.$inject = ['service1', '$stateParams'];
/**
* Controller 1
* @constructor
*/
function Controller1(service1, $stateParams) {
// Declare self and variables
var vm = this;
vm.number = 0;
init();
/**
* Initializes the controller
*/
function init() {
service1.refreshCount($stateParams.id);
vm.number = service1.getCount();
}
}
})();
以下は、変数と Service2 にある関数への参照のみを格納する Service 1 です。
(function() {
'use strict';
angular
.module('app.event')
.factory('service1', service1);
service1.$inject = ['service2'];
/**
* The event index service
* @constructor
*/
function service1(service2) {
// Declare
var count = 0;
// Create the service object with functions in it
var service = {
getCount: getCount,
setCount: setCount,
refreshCount: refreshCount
};
return service;
///////////////
// Functions //
///////////////
/**
* Refreshes the count value
*/
function refreshCount(id) {
service2.getCounts(id).then(
function (response) {
setCount(response.data.Count);
});
}
/**
* Returns the count value
*/
function getCount() {
return count;
}
/**
* Updates the count
* @param {int} newCount - The new count value
*/
function setCount(newCount) {
count = newCount;
}
}
})();
以下は、関数が Service1 から呼び出される Service2 の一部です。
// Gets a count from the DB
getCounts: function(id) {
$http({ url: APP_URLS.api + 'WebApiMethodName/' + id, method: 'GET' }).then(function (response) {
return response.data.Count;
});
},
etc : function() {},
etc : function() {}
これは良い考えですか、それともこの方法でサービス間の通信に何か問題がありますか?