user2877989 user2877989 - 5 months ago 83
AngularJS Question

Ionic fire event on input text value changes

In Ionic, how can you catch an event on a text input field when its value changes?

The input field:

<input type="search" placeholder="Search" ng-text-change="searchMenu()">


The controller:

// ...
$scope.searchMenu = function () {
alert('changed')
console.log(1);
};
// ...


Nothing happens when typing into the text field.

Answer

There are two ways you can do that with ionic (Which is angular in a nutshell)

  1. Use $scope.$watch:

markup:

 <input type="search" placeholder="Search" ng-model="search" />

and code:

  $scope.$watch('search',function (oldValue, newValue) {
     alert('changed');
     console.log(1);
  });
  1. Using ng-change directive togher with ng-model:

markup:

 <input type="search" placeholder="Search" ng-model="search" ng-change="onSearchChange()" />

and code:

 $scope.onSearchChange = function () {
    alert('changed');
    console.log(1);
}
Comments