Jim Peeters
Jim Peeters

Reputation: 2853

How to call a method in angular when an inputfield is empty?

I am now calling controller methods when this inputfield is changing :

<input type="text" ng-model="query" ng-model-options='{ debounce: 500 }' ng-change="searchOnTags(query)"/>

How can I call a different method when the input field is empty again?

Upvotes: 0

Views: 955

Answers (2)

thepio
thepio

Reputation: 6263

You can just use if statement inside your searchOnTags function like this:

$scope.searchOnTags = function(query) {
  console.log(query);
  if (!query) {
    console.log('input is empty');
  }
};

Upvotes: 1

Thalaivar
Thalaivar

Reputation: 23632

Inside your searchOnTags function check whether the form is $pristine, which means the input field is back to its initial state.

When you edit a field it become dirty(ng-dirty), when you clear the input you can set the form to pristine state(ng-pristine).

Upvotes: 1

Related Questions