None
None

Reputation: 9247

How to pass value on enter?

I have directive:

angular.module("mainModule").directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown", function (event) {
            if (event.which === 13) {
                scope.$apply(function () {
                    scope.$eval(attrs.ngEnter);
                });

                event.preventDefault();
            }
        });
    };
});

I have input field :

 <input type="search" ng-model="searchText" ng-model-options="{ debounce: 1000 }" placeholder="@Translator.Translate("SEARCH_OFFER")" ng-keydown="checkKeyDown($event)" ng-change="search()" ng-enter="Search(searchText)">

Problem is when i hit enter i cant pass value searchText to controller its undefined...any suggestion?

EDIT:Its not debaunce because when i remove it i get same result.Any other suggestion?

Upvotes: 1

Views: 137

Answers (2)

Qwertiy
Qwertiy

Reputation: 21380

 ng-model-options="{ debounce: 1000 }"

I expect that you are waiting a second before the scope value changes.

Upvotes: 0

Praveen Singh
Praveen Singh

Reputation: 2569

Try this snippet:

angular.module('testApp', [])

.controller('homeCtrl', ['$scope', function($scope) {
	$scope.getVal = function(event) {
		var commentEl = angular.element(event.target);
		if (event.keyCode === 13) {
			alert(commentEl.val());
		}

	};
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp">
    <section ng-controller="homeCtrl">
        <input ng-keydown="getVal($event)">
    </section>
</body>

Upvotes: 3

Related Questions