私は、コントローラ間で変数を渡すためのサービスを構築しました。変数(vm.organizations)にすべての組織があり、vm.contactsにはlistview.Html(contactControllerの下にある)のデータベース内のすべての連絡先があります。AngularJS:何度も何度も問い合わせる必要がないように、サービスからデータを保存する方法
データサービス:
//dataService.js
(function() {
angular.module('appContacts')
.factory('dataService', ['$http', dataService]);
function dataService($http) {
return {
getAllOrganizations: getAllOrganizations,
getAllAvailableOrganizations: getAllAvailableOrganizations,
getAllAvailableContacts: getAllAvailableContacts,
getOrganizationById: getOrganizationById,
GetContactById: GetContactById,
};
function getAllOrganizations() {
return $http({
method: 'GET',
url: 'api/organizations'
});
}
function getAllAvailableOrganizations() {
return $http({
method: 'GET',
url: 'api/organizations'
});
}
function getAllAvailableContacts() {
return $http({
method: 'GET',
url: 'api/contacts'
});
}
function getOrganizationById(id) {
return $http({
method: 'GET',
url: '/api/organizations/{id}'
});
}
function GetContactById(id) {
return $http({
method: 'GET',
url: '/api/contacts/{id}'
});
}
}
})();
そして、私は2つのコントローラからそれを呼び出す:contactsController.jsを
//listview.html
<html ng-app="My-app">
<body ng-controller="contactsController">
<table class="table table-striped" >
<thead>
<tr>
<th>Name </th>
<th>Company</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in vm.contacts>
<td><a ui-sref="contact({Id: contact.id})" ng-click="vm.setCurrentContact(contact)">{{ contact.lastName}}, {{contact.firstName}} </a></td>
<td><a ui-sref="organization({Id: contact.organizationId})"> {{contact.organization.organizationName}} </a></td>
</tr>
</tbody>
</table>
</body>
</html>
私はサービスのデータベースからデータを取得する(dataservice.js)を構築していますand OrganizationsController.js
私の問題は、別のページに移動するたびに、サービス は、パフォーマンス問題で終了する のデータベースから、すでに持っているすべてのデータを取得する必要があります。そのデータは セッション内では変更されません。
この情報を保存してすべてのコントローラから利用できる方法はありますか?。私は、ローカルストレージについて読んだことがあるが、それは常に$scope
で使用されていて、私が(代わりに$scope.contact
vm.contact)構文ASコントローラを使用しています見るようAngularJSでのサービスがsingleton.Theyあるため
答えていただきありがとうございます。しかし、$ scopeを使用せずにサービスを呼び出すにはどうすればよいですか。私はコントローラASの構文を使用しています –
'vm.orginations = dataService.getCachedOrginations();' $スコープはこれとは関係ありません。 –
@RafaelMunoz私は2番目のオプションで私の答えを更新しました。見てください –