0

I have a function inside my controller that I want to be able to call from a $scope method so I can reuse my code only within the controller. I use the function to clear some input fields. I would like to call after a user is inserted as well as I would like to assign it to the a "Clear" button click. Calling the function after inserting works just fine but assigning the function to the method isn't.

If I assign the function directly to the $scope.clearUser method it works but then I can't call it after inserting a user.

function clearUser() {
    $scope.EmployeeNumber = '';
    $scope.isAdmin = false;
};

$scope.clearUser = clearUser();    
//also tried $scope.clearUser = this.clearUser();

//$scope.clearUser = function() {
//    $scope.EmployeeNumber = '';
//    $scope.isAdmin = false;
//};

<button ng-click="clearUser()" class="btn btn-default">
    <i class="fa fa-times"></i> Clear
</button>

2 Answers 2

6

Omit the () to pass the function:

$scope.clearUser = clearUser;

Or, create it all at once:

var clearUser = $scope.clearUser = function() {

}
Sign up to request clarification or add additional context in comments.

Comments

0

try to change,

$scope.clearUser = clearUser();

to

$scope.clearUser = clearUser;

1 Comment

Perfect! Removing the () worked. I would have never guessed to assign it that way. I would have thought it needed the () to let it know it was a function. Either way, I know for now on.Thanks for everyone's help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.