As a newcomer to Angular, I have created an 'Employee Search' Service module. Let's take a look at the code:
// Employee Search Service
app.service('employeeSearchService', function($http, resourceServerAddress){
this.empList = [];
// Method to clear the employee search list
this.clearEmpList = function(){
this.empList = [];
}
// Method to fetch the employee search list
this.fetchEmpList = function(){
return this.empList;
}
// Method to perform an employee search
this.searchEmpList = function(empName){
var postData = {
empName:empName,
};
$http({
method: 'POST',
url: resourceServerAddress,
data : postData
}).then(function successCallback(response) {
console.log('Response Data : '+response);
if(response['data']['status'] === 'success'){
console.log('Response received successfully with status=success');
if(response['data']['data'].length)
{
console.log('matches found for employee search');
this.empList = response;
}
else
{
console.log('no matches found for employee search');
this.empList = [];
}
}
if(response['data']['status'] === 'error'){
console.log('Response received successfully with status=error');
}
}, function errorCallback(response) {
console.log('Error occur at time of processing request');
});
}
});
Next, in my Controller, I am using the following code to retrieve data from the Service module.
employeeSearchService.searchEmpList(empName);
empSearchResponseList = employeeSearchService.fetchEmpList();
console.log('Data received from server module : '+empSearchResponseList);
While I can see the data and console messages from the service module in Chrome console, I am unable to capture that data in the Controller variable.
I suspect that the way I am using 'searchEmpList()' & 'fetchEmpList()' in my controller may not be correct. I am struggling to find out how to modify it.
Seeking some guidance!
--- Updated Controller Code ---
// Controller for application Home route
app.controller("HomeController", function($scope, $state, $location, $ionicHistory, $ionicSideMenuDelegate, $http, resourceServerAddress, employeeSearchService) {
console.log('At home controller');
// Check application session. If none exists, redirect user to login page
if(window.localStorage.getItem("access_token") === "undefined" || window.localStorage.getItem("access_token") === null) {
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
console.log('Redirecting user to login page');
$state.go("login");
}
$scope.empName = '';
$scope.alertMsgBox = false;
$scope.alertMsgText = '';
$scope.employees = [];
$scope.resourceServerAddress = resourceServerAddress;
var empSearchResponseList=null;
// Method for searching employees
$scope.searchEmployee = function(form){
console.log('Employee name entered : '+$scope.empName);
console.log('Length of employee name : '+$scope.empName.length);
if($scope.empName.length >= 3 ){
var postData = {
Emp_Name:$scope.empName,
access_token:window.localStorage.getItem('access_token'),
session_id:window.localStorage.getItem('session_id')
};
$http({
method: 'POST',
url: resourceServerAddress,
data : postData
}).then(function successCallback(response) {
console.log('Response Data : '+response);
if(response['data']['status'] === 'success'){
console.log('Response received successfully with status=success');
if(response['data']['data'].length)
{
console.log('matches found for employee search');
$scope.employees = response['data']['data'];
$scope.alertMsgBox = false;
}
else
{
console.log('no matches found for employee search');
$scope.alertMsgBox = true;
$scope.employees = [];
$scope.alertMsgText = 'No matches found.';
}
}
if(response['data']['status'] === 'error'){
console.log('Response received successfully with status=error');
}
}, function errorCallback(response) {
console.log('Error occur at time of processing request');
});
}
}
// Method to display employee profile
$scope.showEmpProfile = function(empId){
console.log('Clicked on profile image of employee ID : '+empId);
// Redirect to home page
$state.go('app.emp-profile', {empId:empId});
}
});