I want to clear form after completed some actions with user's input. I know how to do this from the controller. For example, I can run following function:
$scope.clearForm = function () {
$scope.addOrEditForm.$setPristine();
$scope.firstName = "";
$scope.secondName = "";
$scope.position = "";
};
This works. But I need to call this function from another function, which is located in AngularJS service. How I can call it from there? Or how I can define clearForm
function in the service and get access to all these form variables like $scope.firstName
, $scope.secondName
, $scope.position
? Thanks for helping!
Use an event in the controller
// listen for the event in the relevant $scope
$scope.$on('myCustomEvent', function (event) {
$scope.clearForm();
});
Call the event from the service
$rootScope.$broadcast('myCustomEvent');