Angular - 在控制器中使用表单post值

时间:2022-01-13 13:23:05

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

我正在AngularJS中编写我的第一个应用程序,而且我是javascript的新手。我有一个简单的问题:

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

如何在角度控制器中使用html表单中的POST值?

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

我有一个表单(为了便于阅读,我已经删除了css和验证内容):

<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 个解决方案

#1


1  

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

使用ng-model =“email”设置输入时,只需调用$ scope.email即可在控制器中访问这些变量值。

Case-1: For single value

案例1:单值

HTML

HTML

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

Angular Controller

角度控制器

console.log($scope.email);

Case-2: For multiple values

案例2:对于多个值

HTML

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>

#2


1  

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

您需要使用$ scope来绑定ng-model的值,如下所示...

          $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 
                     });

#3


1  

HTML

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

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 

#4


1  

In your html

在你的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 
           });
   };
}

#5


1  

Three ways you can do

你可以采取三种方式

Type 1 With individual params

类型1与个人参数

HTML

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

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

类型2使用对象作为参数

HTML

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

JS

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

Type 3 Using $scope

类型3使用$ scope

HTML

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

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

在这三个演示中查看最佳解决方案的工作演示:https://jsbin.com/rimemi/25/edit?html,js,console,output

#1


1  

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

使用ng-model =“email”设置输入时,只需调用$ scope.email即可在控制器中访问这些变量值。

Case-1: For single value

案例1:单值

HTML

HTML

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

Angular Controller

角度控制器

console.log($scope.email);

Case-2: For multiple values

案例2:对于多个值

HTML

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>

#2


1  

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

您需要使用$ scope来绑定ng-model的值,如下所示...

          $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 
                     });

#3


1  

HTML

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

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 

#4


1  

In your html

在你的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 
           });
   };
}

#5


1  

Three ways you can do

你可以采取三种方式

Type 1 With individual params

类型1与个人参数

HTML

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

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

类型2使用对象作为参数

HTML

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

JS

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

Type 3 Using $scope

类型3使用$ scope

HTML

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

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

在这三个演示中查看最佳解决方案的工作演示:https://jsbin.com/rimemi/25/edit?html,js,console,output