Search code examples
angularjskeypress

Angular - fire function on keypress


I want to fire an event on keypress in angular. At the momemnt I have this input text box:

  <input type="text" class="form-control input-sm"
      name="forenames"
      ng-model-options="{ updateOn: 'blur' }"
      ng-model="record.Forenames"
      ng-keyup="getInitials($event)">

And this function:

$scope.getInitials = function () {
                           if ($scope.record.Forenames != null) {
                               $scope.record.Initials = $filter('genInitials')($scope.record.Forenames.replace(/[^\w\s]|_/g, ""), false);
                           } else {
                               $scope.record.Initials = "";
                           }

                       };

But it doesn't fire. Does anyone have any suggestions for angular?


Solution

  • you can use ng-change for this

    https://docs.angularjs.org/api/ng/directive/ngChange