How to get field value with ng-change

I know how to respond to user input in a text box with ng-change in AngularJS. But how can I get the current input inside an Angular controller? I lost something like $(this).value(); in jQuery.

 <script> angular.module('changeExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.evaluateChange = function() { console.log("How do I get the current content of the field?"); }; }]); </script> <div ng-controller="ExampleController"> <textarea ng-change="evaluateChange()" id="ng-change-example1"></textarea> </div> 
+6
source share
2 answers

ng model

It will save the value of input, text field or select.

Your html should look like this:

 <div ng-controller="ExampleController"> <textarea ng-model="myValue" ng-change="evaluateChange()" id="ng-change-example1"></textarea> </div> 

Then in your controller you can reference this with $scope.myValue

Hope this helps! :)

+10
source

You can use $ event to get the value of the current element. Something like that

 <script> angular.module('changeExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.evaluateChange = function(obj,$event) { var currentElement = $event.target; console.log(currentElement.value);//this will give you value of current element }; }]); </script> <div ng-controller="ExampleController"> <textarea ng-change="evaluateChange(this)" id="ng-change-example1"></textarea> </div> 
+9
source

Source: https://habr.com/ru/post/989431/


All Articles