0

I'm writing my first application in AngularJS, and I'm new to javascript. I have a simple question:

How do I use POST values from an html form in an angular controller?

I have a form (i've stripped out the css and validation stuff for ease of reading):

<form name="signupForm" novalidate>

                <input type="text" placeholder="Name..." ng-model="name" name="name" required />

                <input type="email" name="email" ng-model="email" placeholder="Email..." required>

                <input type="password" placeholder="Password..." ng-model="password" name="password" required ng-minlength="7" ng-maxlength="50"/>

                <button type="button" ng-click="auth.signup()">Sign Up</button>

</form>

I have a controller (no errors), with a function which looks a bit like this:

    function SignupController($http, $scope, $rootScope, $location) {

          vm.signup = function(name, email, password, onSuccess, onError) {

              $http.post('http://myapi.com/api/authenticate/signup',
              {
                  name: name,
                  email: email,
                  password: password
              }).then(function(response) {
                // etc 
              }); 
}

Now, how do I assign the values name, email, password from the form to name, email, password in the controller?

Thanks.

5 Answers 5

1

When you set the input with ng-model="email", then you can access those variable values in controller by just calling $scope.email.

Case-1: For single value

HTML

<input type="text" ng-model="email" />

Angular Controller

console.log($scope.email);

Case-2: For multiple values

HTML

<input type="text" ng-model="user.firstname" />
<input type="text" ng-model="user.lastname" />
<input type="text" ng-model="user.email" />

Angular Controller

//This will print the all the values (firstname, lastname, email) contains user as object.
console.log($scope.user);

Please check this working snippet to see the real time example

var app = angular.module('myApp', []);
app.controller('FormCtrl', function ($scope, $http) {
  $scope.user = {};
  $scope.signup = function(){
      console.log($scope.user);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <form name="signupForm" ng-controller="FormCtrl" ng-submit="signup()">
  <input type="text" placeholder="Name..." ng-model="user.name" name="name" required />
  <input type="email" name="email" ng-model="user.email" placeholder="Email..." required>
  <input type="password" placeholder="Password..." ng-model="user.password" name="password" required ng-minlength="7" ng-maxlength="50"/>
  <button type="submit">Sign Up</button>
  </form>
</body>

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

Comments

1

You need to use $scope for binding ng-model's value as shown as follow...

          $scope.signup = function() {
             data={
                      name: $scope.name,
                      email: $scope.email,
                      password: $scope.password
                  }
              $http.post('http://myapi.com/api/authenticate/signup',data).then(function(response) {
                // etc 
                     });

Comments

1

HTML

<form name="signupForm" novalidate>

            <input type="text" placeholder="Name..." ng-model="user.name" name="name" required />

            <input type=“email" name="email" ng-model="user.email" placeholder="Email..." required>

            <input type="password" placeholder="Password..." ng-model="user.password" name="password" required ng-minlength="7" ng-maxlength="50"/>

            <button type="button" ng-click="auth.signup()">Sign Up</button>

JS

function SignupController($http, $scope, $rootScope, $location) {
   $scope.user={};
  vm.signup = function(name, email, password, onSuccess, onError) {

      $http.post('http://myapi.com/api/authenticate/signup',
      {
          name: $scope.user.name,
          email: $scope.user.email,
          password: $scope.user.password
      }).then(function(response) {
        // etc 

Comments

1

In your html

<form name="signupForm" novalidate ng-submit="vm.signup()">
    <input type="text" placeholder="Name..." ng-model="name" name="vm.user.name" required />
    <input type=“email" name="email" ng-model="vm.user.email" placeholder="Email..." required>
    <input type="password" placeholder="Password..." ng-model="vm.user.password" name="password" required ng-minlength="7" ng-maxlength="50"/>
    <button type="submit">Sign Up</button>
</form>

In your controller

function SignupController($http, $scope, $rootScope, $location) {
   vm.user = {};

   vm.signup = function() {
       // validate here

       // send data
       $http
          .post('http://myapi.com/api/authenticate/signup', vm.user)
          .then(function(response) {
            // handle success 
           });
   };
}

Comments

1

Three ways you can do

Type 1 With individual params

HTML

   <form name="signupForm" novalidate>
     <input type="text" placeholder="Name..." ng-model="name" name="name" required />
     <input type="email" name="email" ng-model="email" placeholder="Email..." required />
     <input type="password" placeholder="Password..." ng-model="password" name="password" ng-minlength="7" ng-maxlength="50" required/>
     <button type="button" ng-click="auth.signup(name, email, password)">Sign Up</button>
  </form>

JS

vm.signup = function(name, email, password) {
      $http.post('http://myapi.com/api/authenticate/signup',
      {
          name: name,
          email: email,
          password: password
      }).then(function(response) { });              
    }

Type 2 With object as param

HTML

     <form name="signupForm" novalidate>
       <input type="text" placeholder="Name..." ng-model="user.name" name="name" required />
      <input type="email" name="email" ng-model="user.email" placeholder="Email..." required />
      <input type="password" placeholder="Password..." ng-model="user.password" name="password" ng-minlength="7" ng-maxlength="50" required/>
      <button type="button" ng-click="auth.signup(user)">Sign Up</button>
    </form>

JS

vm.signup = function(data) {
      $http.post('http://myapi.com/api/authenticate/signup', data)
        .then(function(response) {
      });              
  }

Type 3 Using $scope

HTML

     <form name="signupForm" novalidate>
       <input type="text" placeholder="Name..." ng-model="user.name" name="name" required />
       <input type="email" name="email" ng-model="user.email" placeholder="Email..." required />
       <input type="password" placeholder="Password..." ng-model="user.password" name="password" ng-minlength="7" ng-maxlength="50" required/>
       <button type="button" ng-click="auth.signup()">Sign Up</button>
    </form>

JS

vm.signup = function() {
        $http.post('http://myapi.com/api/authenticate/signup', $scope.data)
            .then(function(response) {
      });              
 }

It will work in all these ways.

Checkout the working demo of best solution among these three Demo: https://jsbin.com/rimemi/25/edit?html,js,console,output

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.