vdegenne
vdegenne

Reputation: 13270

how to reference the value of an input element using angular event handler

how I do :

<input ng-keyup="foo($event)">

$scope.foo = function (event) {
  var v = event.srcElement.value;
}

how I want to do :

<input ng-keyup="foo(this.value);">

$scope.foo = function (v) {
  var v = v;
}

this doesn't work. this doesn't reference to the input element.

Upvotes: 0

Views: 159

Answers (2)

Vinay K
Vinay K

Reputation: 5572

For all the ng- events(or other directives) types this means the scope with which the DOM is compiled.

Bind the input's value to a model and use in your event handler.

<input ng-model="myModel" ng-keyup="foo()">

In Controller

$scope.foo = function () {
    //use $scope.myModel
}

If you don't want to bind the input to a model, the solution which you are using currently is fine.

Upvotes: 1

Sam Jacobs
Sam Jacobs

Reputation: 346

You should bind the value of the input to the $scope using ng-model and either pass that value in as a parameter to foo or you can reference it directly in the definition of $scope.foo.

Upvotes: 0

Related Questions