简体   繁体   中英

$event not defined in ng-change on text input

I hope my issue is simple to solve, I can't access the value by ng-model because i have multiple of these boxes as they are rendered as part of a list of inputs in a form. I am trying to get the ID and text value of a text box with ng-change . heres my html:

<input type="text" class="other-box" ng-model="test" id="4" ng-change="otherBoxUpdate(this)"/> 

(ng-model is required, which is why it's in there). Hers is my controller snippet:

$scope.otherBoxUpdate = function (obj, $event) {

    console.log(obj)
    console.log($event)
    console.log($event.target)
}

obj seems to return a scope value, however from what i've read I need to access $event.target , however $event is not defined. What am i doing wrong?

ng-change not allow to pass $event as parameter.

 ng-change="otherBoxUpdate(test)"

 var myapp = angular.module('app', []); myapp.controller('Ctrl', function ($scope) { $scope.otherBoxUpdate = function (obj) { console.log(obj); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="Ctrl as vm"> <input type="text" class="other-box" ng-model="test" id="4" ng-change="otherBoxUpdate(4)"/> </div> 

If you only need to identify the input that was clicked, you can pass some other piece of identifying information to your handler. For example, we can pass the id:

<input type="text" class="other-box" ng-model="test" id="4" ng-change="otherBoxUpdate(4)"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM