Standard Way (industry)
Make a js folder. Inside this folder make another folder name it utils.
As $http request will be made several times it is better to keep it separately. Make an AngularJS factory file and call it jsonLoader.js.
jsonLoader.js:
app.factory("jsonLoader",function($http,$q){
var object={
loadJson(json){
var defer=$q.defer();
//json url of ur data
var jsonUrl="http://localhost:3000/jsonData/"+json+".json";
$http.get(jsonUrl).then(function(resolveData){
defer.resolve(resolveData);//recivedData
},
function(rejectedError){
console.log("Rejected error from jsonLoader ",rejectedError);
}
);
return defer.promise;
}
}
return object;
});
Now in your controller inject this factory like this:
app.controller("mainController",function($scope,mainFactory,jsonLoader){
$scope.loadNavMenu=()=>{
console.log("controller");
var promise =jsonLoader.loadJson('navMenu');
promise.then(function(recivedData){
console.log("Data is ",recivedData.data);
//callback function here or some data binding
$scope.menuItems=recivedData.data.menuItems;
});
}
}
method: GETis already defined for you. So, any $http() examples you see in the docs that use promises also apply to $http.get().