1

I have a function reset(username) that outputs whatever is entered into the input field of ng-model="username". Why is it not appearing in console though?

This is my function

$scope.reset = function (username) {
   console.log(username);
};

and the way I submit the form

<form name="Form" ng-controller="ResetController" ng-submit="reset(username)">
<div>
   <div class="row top5">        
      <label for="username">Username</label>
      <div class="col-xs-4">
         <input type="text" id="username"  placeholder="" maxlength="255"
          ng-model="username" required autofocus>
      </div>  

   <div>
      <div class="col-xs-5">          
         <button translate class="btn btn-secondary" type="submit">Reset</button>
      </div>
   </div>
</div>   
</form>

Controller as requested

app.controller("ResetController", function ($scope) {
   $scope.username='';
   $scope.reset = function (username) {
      console.log('username = ', username);
   };
});
LazioTibijczyk
  • 1,701
  • 21
  • 48

1 Answers1

2

I believe you do have the ng-app and everything else besides this form, however it is better handled by the DOM if you manipulate your inputs as properties of an object and not directly as $scope. properties. See answers to this here. And note in the code how I attached username to $scope.object.username

var app = angular.module('myApp',[])

app.controller('ResetController',['$scope',function($scope){

   $scope.object = {};
   $scope.reset = function(username){
    console.log(username);
   };
   
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <form name="Form" ng-controller="ResetController" ng-submit="reset(object.username)">
  <div>
     <div class="row top5">        
        <label for="username">Username</label>
        <div class="col-xs-4">
           <input type="text" id="username"  placeholder="" maxlength="255"
            ng-model="object.username" required autofocus>
        </div>  

     <div>
        <div class="col-xs-5">          
           <button translate class="btn btn-secondary" type="submit">Reset</button>
        </div>
     </div>
  </div>  
  </form>
</div>
Andrew Adam
  • 1,572
  • 9
  • 22