I am pretty new to angularjs and I want to know what is the best practice for this kind of problem. I have a function in my controller1 used for validation of registration form which is called from my uniqueField directive which is attribute:
$scope.validation=function(param){
$scope.loading[param.field]=true;
var currentPath=$location.path(); //current route
//function for ajax web call
var webCall = $http({
method: 'POST',
url: currentPath+'/validation',
async : true,
headers: {
'Content-Type': 'application/json'
},
timeout:10000,
data: param});
webCall.then(handleSuccess,handleError); //server call
function handleSuccess(response) { //successful web call handler
$scope.loaded[param.field]={};
$scope.loading[param.field]=false;
if(response.data.status===1) {
$scope.loaded[param.field]["available"]=true;
$scope.loaded[param.field]["error"]=false;
$scope.loaded[param.field]["message"]=response.data.message;
}
else if(response.data.status===0){
$scope.loaded[param.field]["available"]=false;
$scope.loaded[param.field]["error"]=false;
$scope.loaded[param.field]["message"]=response.data.message;
}
}
function handleError(response){
$scope.loaded[param.field]={};
$scope.loading[param.field]=false;
$scope.loaded[param.field]["error"]=true;
$scope.loaded[param.field]["Message"]="Cannot fetch data from server";
};
}
}
Now I want similar functionality in next controller too. What would be the best practice so that I dont have to redefine the function again in another controller?