I'm new to Angular and maybe I'm missing some of the concept. My idea was to separate Angular Controllers logic from the UI logic, meaning that the controllers could be easily reusable withouth knowing what the UI would do with tha data it processed. In this caso, I would like to, for example, clear the form and inputs errors outside the controller.
Imagien I have a controller functiuon like this:
$scope.createCategory = function () {
$scope.Loading = true;
$scope.clearServerError();
$scope.categoryForm.$setPristine();
angular.forEach($scope.categoryForm, function (input) {
if (input && input.hasOwnProperty('$viewValue')) {
input.$setPristine();
input.$setUntouched();
}
});
$("#categoryForm :input").prop("disabled", true);
var response = $http({
url: "/api/categories",
method: "POST",
headers: authHeaders,
data: JSON.stringify($scope.NewCategory),
}).then(function mySuccess(response) {
$scope.Categories.push(response.data);
$scope.resetNewCategory();
$('#category-add-modal').modal('hide');
}, function myError(response) {
var message = response.data.error_description;
$scope.Server.error = true;
$scope.Server.message = message;
}).finally(function end() {
$scope.Loading = false;
$("#categoryForm :input").prop("disabled", false);
});
};
As you can see, the controller nows some aspects of the UI, like the form is named "categoryForm" and there's a modal called "#category-add-modal". I wanted to separete this logic, passing 4 functions callbacks: initCallback, successCallback, failureCallback and finallyCallback. Only these three functions would know what elements the UI has. In this case, and for example, the initCallback should reset category form and all inputs to Pristine and untouched. How is that achievable?