【问题标题】:How to send data from a text box from one form to the other in angularjs?如何在angularjs中将文本框中的数据从一种形式发送到另一种形式?
【发布时间】:2015-03-18 18:47:42
【问题描述】:

我正在使用此示例表单来发送数据。我目前在控制台下观看。但我真的需要将这些信息发送到另一个名为 result.html 的页面,我想知道如何做到这一点。目前我在主页中使用以下代码...这是代码

<!-- File: chapter4/two-forms-databinding.html -->
<html ng-app="notesApp">
<head><title>Notes App</title></head>
<body ng-controller="MainCtrl as ctrl">

  <form ng-submit="ctrl.submit1()">
    <input type="text" ng-model="ctrl.username">
    <input type="password" ng-model="ctrl.password">
    <input type="submit" value="Submit">
  </form>

  <form ng-submit="ctrl.submit2()">
    <input type="text" ng-model="ctrl.user.username">
    <input type="password" ng-model="ctrl.user.password">
    <input type="submit" value="Submit">
  </form>

<script
  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
  angular.module('notesApp', [])
    .controller('MainCtrl', [function() {
      var self = this;
      self.submit1 = function() {
        // Create user object to send to the server
        var user = {username: self.username, password: self.password};
        console.log('First form submit with ', user);
      };
      self.submit2 = function() {
        console.log('Second form submit with ', self.user);
      };
    }]);
</script>
</body>
</html>

我想知道result.html页面中的代码应该如何抓取这些表单发送的参数。请帮助我,我对 angularjs 真的很陌生。

【问题讨论】:

    标签: html angularjs forms


    【解决方案1】:

    <!-- File: chapter4/two-forms-databinding.html -->
    <html ng-app="notesApp">
    <head><title>Notes App</title></head>
    <body ng-controller="MainCtrl as ctrl">
    
      <form ng-submit="ctrl.submit1()">
        <input type="text" ng-model="ctrl.username">
        <input type="password" ng-model="ctrl.password">
        <input type="submit" value="Submit">
      </form>
    
      <form ng-submit="ctrl.submit2()">
        <input type="text" ng-model="ctrl.user.username">
        <input type="password" ng-model="ctrl.user.password">
        <input type="submit" value="Submit">
      </form>
    
    <script
      src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
    </script>
    <script type="text/javascript">
      angular.module('notesApp', [])
        .controller('MainCtrl', [function() {
          var self = this;
          self.submit1 = function() {
            // Create user object to send to the server
            self.user = {username: self.username, password: self.password};
            console.log('First form submit with ', self.user);
          };
          self.submit2 = function() {
            console.log('Second form submit with ', self.user);
          };
        }]);
    </script>
    </body>
    </html>

    【讨论】:

    • 这是修改后的代码,通过运行在提交第一个表单检查时将数据填充到第二个表单
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多