I online read that you should a use Service for getting data instead of simply putting the code in the controller, in order to keep Controllers thin.
Here's my Controller, which fetches a list of employees:
angular.module("app").controller("MyController", function ($scope, $http) {
$http.get("/api/getempl").then(function (response) {
if (response.status == 200) {
$scope.empData = response.data.data;
} else {
console.log('400');
}
});
});
Then I tried the _Service approach in hope for improved performance
angular.module("app").factory("testService", function ($http, $location) {
return {
getData: function () {
var promise = $http.get("/api/getempl").then(function (response) {
return response.data.data;
});
return promise;
}
};
});
Now when I injected the Service like as shown up, and tested it in firebug under net tab, there's no improvement in page load time but it rather increased instead.
What am I doing wrong in the code, or what concept am I missing with the usage of Services in AngularJS?
angular.module("app").controller("MyController", function ($scope, $http, testService) {
testService.getData().then(function (response) {
$scope.empData = response;
});
});
ServiceorResourcefor my api hits for better performance ?