0

I use a lot of forms in my app, but for some reason, one of them (the simplest one, actually) doesn't work properly. When I submit it, the value of the input which is given by my console.log is "undefined".

I'm pretty sure it's a very simple mistake, but I can't see it.

I tried to use "form = 'forgottenForm' " in my inputs, I also tried ng-model but it's useless since I'm not displaying the value of "email", I just want to submit it and then perform some database operations.

HTML

<form name="forgottenForm" id='forgottenForm' novalidate="" ng-controller="forgottenCtrl">
    <label class="forgotten-item item item-input">
        <input type="text" name="forgottenEmail" required>
    </label>
    <button type="submit" class="button" ng-click="submitForgotten(forgottenEmail)">Valider</button>
</form>

JavaScript

angular.module('ionicApp').controller("forgottenCtrl", function($scope, $http, $ionicPopup, $cookies, $rootScope, $window, $q){
    $scope.submitForgotten = function(forgottenEmail){
        console.log(forgottenEmail);
    }
})
5
  • 6
    change to ng-model="forgottenEmail" (you can also have ng-submit on the form itself instead of ng-click on your button) Commented Dec 20, 2017 at 14:25
  • Yeah that was it, I don't know why I figured that the parameter read was "name" and not "ng-model". Thank you for that. Commented Dec 20, 2017 at 14:26
  • @AlekseySolovey, you should change that comment to an answer :) Commented Dec 20, 2017 at 14:58
  • 2
    @SteveDanner ye, but nah. points < (quick) help Commented Dec 20, 2017 at 15:02
  • 1
    While I like the spirit and agree with you to a degree, a true, good answer (such as yours) marked as THE answer by the OP may help many others in the future. Commented Dec 20, 2017 at 18:11

2 Answers 2

1

This will work,

  1. You have to declare a variable in $scope like $scope.email=''
  2. Then you need to set it like this <input type="text" ng-model='email' name="forgottenEmail" required>

Actually you don't need to pass the variable submit function

angular.module('ionicApp').controller("forgottenCtrl", function($scope, $http, $ionicPopup, $cookies, $rootScope, $window, $q) {
  $scope.email = '';
  $scope.submitForgotten = function() {
    console.log($scope.email);
  }
})
<form name="forgottenForm" id='forgottenForm' novalidate="" ng-controller="forgottenCtrl">
  <label class="forgotten-item item item-input">
     <input type="text" ng-model='email' name="forgottenEmail" required>
  </label>
  <button type="submit" class="button" ng-click="submitForgotten()">Valider</button>
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

I give you a "thumb up" because I know this works, but I don't mark the answer as accepted, because I was waiting for something which doesn't change the structure of the form (someone corrected me in the comments). But thank you for your answer. :)
0

The answer was to change to ng-model="forgottenEmail".

It is also possible to have ng-submit on the form itself instead of ng-click on the button.

Thanks to Aleksey Solovey for the answer! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.