Angularjs: Picking input value in keypress

Asked

Viewed 644 times

0

I’m already detecting when the user presses enter in the input, but how can I capture the text he typed?

HTML

<div ng-app="Insidetv" ng-controller="ListaController">
     @foreach($listas as $lista)
          <input value="{{ $lista->descricao }}" type="text" name="descricao" ng-keypress="editName($event)">
     @endforeach    
</div>

JS

angular.module('Insidetv')
   .controller('ListaController', function($scope, $http) {

   $scope.editName = function($event){
        var keyCode = $event.which || $event.keyCode;

        if(keyCode === 13) {
             console.log($event);
        }
    };
});

1 answer

0

follow the example

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="/angular.min.js"></script>
    <script>
        const app = angular.module('Insidetv', []);

        app.controller('ListaController', [
            '$scope',
            function($scope) {
                $scope.editName = function($event, model) {
                    var keyCode = $event.which || $event.keyCode;

                    if (keyCode === 13) {
                        console.log(model);
                        console.log($event);
                    }
                };

            }
        ]);
    </script>
</head>

<body>
    <div ng-controller="ListaController">
        <input type="text" name="descricao" ng-model="$scope.model" ng-keypress="editName($event,$scope.model)">
    </div>
</body>

</html>
  • The input is inside a foreach, so there are several of them, as I can capture the value only of what I write?

  • put in the rest of the code

  • Okay, I edited the question. Basically the input is inside a foreach.

  • @Diegovieira replaces the foreach with ng-repeat, with track by $index. Your $scope will have an array type variable with each input value. Then each input can be ngModel an expression of the kind $scope.model[$index]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.