Currently, I have a controller making use of http.get, http.push and http.post methods within my AngularJS app.
During my learning journey with AngularJS, I've discovered that it's more efficient to place http.get calls in service files. While I can easily handle simple http.get requests, I find myself struggling with http.get by id or when passing parameters for http.get/http.post calls.
Here is a snippet of my current controller:
angular.module("app-complaints")
.controller("clcontrol", clcontrol);
function clcontrol($routeParams, $http, $scope) {
$http.get(baseURL + "/api/complaints/" + $scope.complaintCase + "/checklists")
.then(function (cl) {
//success
$scope.index = 0;
$scope.cl = [];
$scope.cl = cl;
I aim to refactor it like this:
controller.js
angular.module("app-complaints")
.controller('clcontrol', function ($http, $scope, $q, Service, $timeout) {
....
getCL();
function getCL(){
Service.getCl()
.success(function(cl){
$scope.cl = [];
$scope.cl = cl;
}
service.js
angular.module("app-complaints")
.factory('Service', ['$http', function ($http) {
Service.getCL = function () {
return $http.get(urlBase + "/api/complaints/" + complaintCase + "/checklists")
};
};