call function on hit enter in input search field

My Html code:

<div class="input-group">
                <input type="text" class="form-control" placeholder="Enter Text" ng-model="csrModel.accountNumber" />
                <span class="input-group-addon" ng-click="csrModel.search(csrModel.accountNumber)">
                    <i class="fa fa-search"></i>
                </span>
            </div>

I have input box which I am using as a search field. On click of span i am calling ng-click function. Same think I want when user will enter after some text in input field i need to call ng-click function.

enter image description here

Please suggest me what to use. I tried to use some form element but it screw my design. I don’t wanna change look and design.

Answer

You can use

<input type="text" class="form-control" placeholder="Enter Text" 
ng-model="csrModel.accountNumber" 
ng-keyup="callOnEnter($event,csrModel.accountNumber)"/>

and in JS

$scope.callOnEnter = function($event, accountNumber) {
 if ($event.keyCode === 13) {
   $scope.csrModel.search(accountNumber);
 }
}

It is more angular way

Leave a Reply

Your email address will not be published. Required fields are marked *