如何使用 AngularJS 处理单个页面中存在的多个表单 [英] How to handle multiple forms present in a single page using AngularJS

查看:19
本文介绍了如何使用 AngularJS 处理单个页面中存在的多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手.我在一个页面上有多个表单,这些表单一次只根据用户选择显示一个.

DOM 有两个子控制器,分别是 FirstFormController, SecondFormController 包裹在名为 XceptionController 的父控制器之下.父控制器函数用于提交来自两个孩子的表单数据.表单数据保存到父控制器的范围内.我的 HTML 如下所示

<div class="container" ng-controller="XceptionController"><form class="form-container"><select id="select-form" ng-change=selectForm() ng-model="selectedForm"><option value="select" disabled selected>选择一个选项</option><option value="firstform">获取名字</option><option value="secondform">获取姓氏</option></选择></表单><div ng-controller="FirstFormController" class="firstform" ng-show="fname"><form name="firstnameform"><input type="text" name="firstname" ng-model="form.firstname" id="firstname"><label for="#firstname">名字</label></表单><div class="content" ng-show="fname"><p>名字是 {{form.firstname}}</p>

<div ng-controller="SecondFormController" class="secondform" ng-show="lname"><表格名称="姓氏表格"><input type="text" name="lastname" ng-model="form.lastname" id="lastname"><label for="#lastname">Lastname</label></表单><div class="content" ng-show="lname"><p>姓氏是 {{form.lastname}}</p>

<button ng-click="submitForm(form)">提交</button>

我的js看起来像

var app = angular.module('app', []);app.controller('XceptionController', function($scope){$scope.form = {};$scope.selectedForm = '';$scope.selectForm = function() {$scope.lname = 0;$scope.fname = 0;var foo = angular.element(document.querySelector('#select-form')).val();if(foo == 'firstform') {$scope.fname = 1;}否则 if(foo == 'secondform'){$scope.lname = '1';}};$scope.submitForm = 函数(表单){//表单数据控制台日志(表单);};});app.controller('FirstFormController', function($scope){$scope.firstname = "";});app.controller('SecondFormController', function($scope){$scope.lastname = "";});

但是在提交表单时,我从两个表单中获取数据,因为我将其设置在父级的范围内.有没有一种方法可以提交表单并仅获取当前显示的表单的表单数据.这个小提琴将帮助你更多地理解我的问题.https://jsfiddle.net/xmo3ahjq/15/

还可以帮助我了解我的代码是否按照应有的方式正确编写,或者是否有更好的方法来实现这一点.表单提交代码是否应该放在单独的 angular 服务下?

解决方案

var app = angular.module('app', []);app.controller('XceptionController', function($scope) {$scope.form = {};$scope.selectedForm = null;$scope.selectForm = function() {//重置表单模型对象$scope.form = {};};$scope.isSelected = function(formName) {返回 $scope.selectedForm === formName;};$scope.submitForm = 函数(表单){//表单数据控制台日志(表单);};});app.controller('FirstFormController', function($scope) {});app.controller('SecondFormController', function($scope) {});

<div class="container" ng-controller="XceptionController"><form class="form-container"><select id="select-form" ng-change=selectForm() ng-model="selectedForm"><option value="" disabled selected>选择一个选项</option><option value="firstform">获取名字</option><option value="secondform">获取姓氏</option></选择></表单><div ng-controller="FirstFormController" class="firstform" ng-show="isSelected('firstform')"><form name="firstnameform"><input type="text" name="firstname" ng-model="form.firstname" id="firstname"><label for="#firstname">名字</label></表单><div class="内容"><p>名字是 {{form.firstname}}</p>

<div ng-controller="SecondFormController" class="secondform" ng-show="isSelected('secondform')"><表格名称="姓氏表格"><input type="text" name="lastname" ng-model="form.lastname" id="lastname"><label for="#lastname">Lastname</label></表单><div class="内容"><p>姓氏是 {{form.lastname}}</p>

<button ng-click="submitForm(form)">提交</button>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

I am a newbie in AngularJS. I am having multiple forms on a single page which gets displayed based on the user selection only one at a time.

The DOM has two child controllers namely FirstFormController, SecondFormController wrapped under a parent controller named XceptionController. A parent controller function is used to submit the form data from both the children. The form data is saved on to the parent controller's scope.My HTML looks like below

<div ng-app="app">
 <div class="container" ng-controller="XceptionController">
<form class="form-container">
  <select id="select-form" ng-change=selectForm() ng-model="selectedForm">
    <option value="select" disabled selected>Select an Option</option>
    <option value="firstform">Get Firstname</option>
    <option value="secondform">Get Lastname</option>
  </select>
</form>

<div ng-controller="FirstFormController" class="firstform" ng-show="fname">
  <form name="firstnameform">
    <input type="text" name="firstname" ng-model="form.firstname" id="firstname">
    <label for="#firstname">Firstname</label>
  </form>
  <div class="content" ng-show="fname">
    <p>Firstname is {{form.firstname}}</p>
  </div>
</div>
<div ng-controller="SecondFormController" class="secondform" ng-show="lname">
  <form name="lastnameform">
    <input type="text" name="lastname" ng-model="form.lastname" id="lastname">
    <label for="#lastname">Lastname</label>
  </form>
  <div class="content" ng-show="lname">
    <p>Lastname is {{form.lastname}}</p>
  </div>
</div>
<button ng-click="submitForm(form)">Submit</button>

And my js looks like

var app = angular.module('app', []);
app.controller('XceptionController', function($scope){
    $scope.form = {};
    $scope.selectedForm = '';
    $scope.selectForm = function() {
    $scope.lname = 0;
    $scope.fname = 0;
    var foo = angular.element(document.querySelector('#select-form')).val();
    if(foo == 'firstform') {
        $scope.fname = 1;
    }
    else if(foo == 'secondform'){
        $scope.lname = '1';
    }
  };
 $scope.submitForm = function(form){
    //form data
    console.log(form);
 };
});

app.controller('FirstFormController', function($scope){
 $scope.firstname = "";
});

app.controller('SecondFormController', function($scope){
 $scope.lastname = "";
});

But on submitting the form I get the data from both the forms since I set it on the parent's scope. Is there a way by which I can submit the form and get the form data only for the form which is currently displayed. This fiddle will help you more in understanding my question. https://jsfiddle.net/xmo3ahjq/15/

Also help me in knowing if my code is properly written the way it should be or is there a better way to implement this. Should the form submission code be placed under a separate angular service ?

解决方案

var app = angular.module('app', []);
app.controller('XceptionController', function($scope) {
  $scope.form = {};
  $scope.selectedForm = null;
  $scope.selectForm = function() {
    // reset the form model object
    $scope.form = {};
  };
  $scope.isSelected = function(formName) {
    return $scope.selectedForm === formName;
  };
  $scope.submitForm = function(form) {
    // form data
    console.log(form);
  };
});

app.controller('FirstFormController', function($scope) {

});

app.controller('SecondFormController', function($scope) {

});

<div ng-app="app">
  <div class="container" ng-controller="XceptionController">
    <form class="form-container">
      <select id="select-form" ng-change=selectForm() ng-model="selectedForm">
        <option value="" disabled selected>Select an Option</option>
        <option value="firstform">Get Firstname</option>
        <option value="secondform">Get Lastname</option>
      </select>
    </form>

    <div ng-controller="FirstFormController" class="firstform" ng-show="isSelected('firstform')">
      <form name="firstnameform">
        <input type="text" name="firstname" ng-model="form.firstname" id="firstname">
        <label for="#firstname">Firstname</label>
      </form>
      <div class="content">
        <p>Firstname is {{form.firstname}}</p>
      </div>
    </div>
    <div ng-controller="SecondFormController" class="secondform" ng-show="isSelected('secondform')">
      <form name="lastnameform">
        <input type="text" name="lastname" ng-model="form.lastname" id="lastname">
        <label for="#lastname">Lastname</label>
      </form>
      <div class="content">
        <p>Lastname is {{form.lastname}}</p>
      </div>
    </div>
    <button ng-click="submitForm(form)">Submit</button>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

这篇关于如何使用 AngularJS 处理单个页面中存在的多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆