AngularJS를 사용하여 직원 기록을 보여주고 있습니다. 두 개의 뷰를 사용하여 데이터를 표시 emp-box.htm
하고 있으며 두 개의 뷰 와 해당 컨트롤러 ( empController
)를 사용하고 employeeBoxController
있으며이 컨트롤러 에서 서비스에서 데이터를 가져오고 employeeBoxController
있으며 에서 얻은 결과를 사용 empController
하여 뷰 ( emp-list.htm
)에 표시 하고 싶습니다. 서비스를 만들었습니다.eService
app.service('dataService',function() {
var s = {};
this.setData = function(data,key) {
s[key]=data;
},
this.getData = function(key) {
return s[key];
}
this.hello = function() {
return 'hello';
}
})
결과를 가져오고 데이터를 설정 employeeBoxController
하고 empController
들어올 때 console.log(dataService.getData('result'));
empController에서 사용하여 데이터를 가져올 때undefined
은 employeeBoxController
이다
app.controller("employeeBoxController", ['$scope', 'employeeService',
'dataService', function($scope, employeeService, dataService) {
$scope.getEmployeeDetails = function(eid) {
$scope.isLoading = false;
employeeService.getDetails($scope.eid).then(function(result) {
dataService.setData(result, 'result');
$scope.isLoading = true;
console.log(dataService.getData('result'));
})
}
}])
가 empController
있다 : -
app.controller("empController", ['$scope', 'employeeService', 'dataService',
function($scope, employeeService, dataService) {
$scope.result = dataService.getData('result');
//console.log(dataService.hello());
console.log(dataService.getData('result'));
console.log(dataService.hello());
}
])
서비스 클래스 employeeService
는 다음과 같습니다.
app.config(["employeeServiceProvider",function(employeeServiceProvider){
employeeServiceProvider.config('http://localhost:8080/pos');
}]);
app.provider("employeeService",function(){
var myurl='';
this.config=function(eurl){
myurl=eurl;
}
this.$get=['$http','$log',function($http,$log){
var employeeobj={};
employeeobj.getDetails=function(eid){
return $http.get(myurl+'/getEmployees/'+eid);
}
return employeeobj;
}];
});
emp-box.htm
is :-
<div>
Enter the id: <input type="text" ng-model="eid"/>
<button ng-click="getEmployeeDetails()">show</button>
</div>
emp-list.htm is:-
<div class="panel panel-primary">
<div class="panel-body" style="text-align:center; margin:0 auto">
<h3>Employee Data</h3>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-body">
<!-- <div ng-show="!isLoading" style="color:red">
<span class="glyphicon glyphicon-time"></span>Loading...
</div>-->
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>empno</th>
<th>salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="oemp in result.data">
<td>{{oemp.eid}}</td>
<td>{{oemp.name}}</td>
<td>{{oemp.empno}}</td>
<td>{{oemp.sal}}</td>
</tr>
</tbody>
</table>
</div>
</div>
내가 이해 한 바에 따르면 API 결과를 다른 서비스에 캐시하려고하므로 다른 컨트롤러에서 API를 다시 호출 할 필요가 없습니다. 또한 empController
먼저 실행되고 dataService.getData('result')
설정의 API 응답을 할 때 아직 수신되지 않고 다른 서비스에서 호출됩니다. 이 두 서비스를 결합하여 서비스의 정확한 값을 캐싱하는 대신 API 호출 자체를 캐시하고 캐시에 데이터가 포함되지 않은 경우 API를 호출하여 캐시 할 수 있도록하는 것이 좋습니다.
여기 제가 만들 수있는 것이 CacheAPIService
있습니다.
app.factory('CacheAPIService', ['$http', function($http) {
var cache = {};
return {
get: function(api) {
if(angular.isUndefined(cache[api])) {
cache[api] = $http.get(api); //storing in cache while making API call
}
return cache[api]; //Return from cache
},
clear: function(api) {
delete cache[api];
}
}
}]);
따라서 캐시를 API 호출로 만들어야 할 때마다이 서비스를 API 호출과 함께 사용하면 캐시도 수행됩니다. 그리고 이미 캐시 된 경우 새 API 호출이 수행되지 않습니다. 이점은 undefined
약속을 반환하는 것처럼 반환되는 경우가 발생하지 않는다는 것 입니다.
첫 번째 컨트롤러에서 업데이트 된 코드는 다음과 같습니다.
app.controller("employeeBoxController", ['$scope', 'CacheAPIService', function($scope, CacheAPIService) {
$scope.getEmployeeDetails = function(eid) {
$scope.isLoading = true;
var endpoint = 'api/endpoint/'+$scope.eid; //Replace with your API endpoint
CacheAPIService.get(endpoint).then(function(result) {
$scope.isLoading = false;
console.log(dataService.getData('result'));
})
}
}]);
여기에서 첫 번째 API 호출이 만들어지고 캐시됩니다. 다른 컨트롤러를 살펴보십시오.
app.controller("empController", ['$scope', 'CacheAPIService', function($scope, CacheAPIService) {
CacheAPIService.get(endpoint).then(function(data) {
var endpoint = 'api/endpoint/'+$scope.eid; //your API endpoint
console.log('data =', data);
});
}]);
여기서 여전히 동일한 서비스를 사용하지만 캐시되고 캐시되지 않은 경우 API 호출을 수행하고 해당 약속을 캐시합니다. 여기서는 캐시에 저장하기위한 키로 API 엔드 포인트를 직접 사용했습니다. 이렇게하면 엔드 포인트 자체가 고유하므로 매번 고유 한 키를 제공 할 필요가 없습니다.
캐시 된 데이터를 삭제하려는 경우 POST 또는 PUT 호출을 할 때 CacheAPIService.clear(apiEndpoint)
캐시에서 세부 정보를 지우도록 호출 할 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다