问题描述:

I have this in controller :

 $scope.SearchOnEnter = function (event) {

var element = angular.element(event.target);

if (event.keyCode === 13) {

_search(element.val());

}

}

In html i have this:

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

But in console im geting error:

Unable to get property 'target' of undefined or null reference

网友答案:

You're passing searchText to the function:

data-ng-enter="SearchOnEnter(searchText)"

but in the function expect it to be an event.

I guess you meant it to be

data-ng-enter="SearchOnEnter($event)"
网友答案:

Check this demo: JSFiddle.

It simply works as expected. Your JSFiddle doesn't work because you do not import AngularJS correctly. Also need to add ng-app="myApp":

<div ng-app="myApp" ng-controller="MyCtrl">
相关阅读:
Top