【问题标题】:add form fields dynamically on user click using angular js使用 Angular js 在用户点击时动态添加表单字段
【发布时间】:2015-12-29 04:02:05
【问题描述】:

添加子按钮不起作用(应在用户单击时添加各种字段(名字、姓名、学生和情况字段)。 详细信息:我正在尝试创建一个包含五个输入字段(名字、中间名、姓氏和两个单选按钮)的表单,如果用户单击添加子按钮应该创建一个新的五个输入字段。

注意:我使用指令来调用此表单)。

 <div ng-controller="Step1Ctrl" layout="column" layout-align="center">
 <div layout-gt-sm="row" layout-align="center">
     <div id="banner">
         <p> <span>STEP 1</span> List all household members who are infants,  children, and students up to and including grade 12</p>
    </div>
</div>
<md-content layout-padding>

    <form name="Form">

        <fieldset layout-gt-sm="row" layout-align="center" data-ng-repeat="child in childern">

            <md-input-container class="md-block">
                <label>First Name</label>
                <input required name="firstName" ng-model="child.firstName">
                <div ng-messages="Form.firstName.$error">
                    <div ng-message="required">First Name is required.</div>
                </div>
            </md-input-container>
            <md-input-container class="md-block" style="max-width:60px;">
                <label>MI</label>
                <input required name=" middleName " ng-model="child.middleName ">
                <div ng-messages="Form.middleName.$error ">
                    <div ng-message="required ">Middle Name is required.</div>
                </div>
            </md-input-container>
            <md-input-container class="md-block ">
                <label>Last Name</label>
                <input required name="lastName " ng-model="child.lastName ">
                <div ng-messages="Form.lastName.$error ">
                    <div ng-message="required ">Last Name is required.</div>
                </div>
            </md-input-container>

            <div class="cell ">
                <label> <b>Student?</b></label>
                <div class="box">
                    <md-radio-group ng-model="child.IsStudent">
                        <md-radio-button value="Yes">Yes</md-radio-button>
                        <md-radio-button value="No"> No </md-radio-button>
                    </md-radio-group>
                </div>
            </div>

            <div class="cell">
                <label><b> Child's situation? </b></label>
                <div class="box">
                    <md-radio-group ng-model="child.situation">
                        <md-radio-button value="Foster Child">Foster Child </md-radio-button>
                        <md-radio-button value="HMR"> Homeless, Migrant, Runaway </md-radio-button>
                    </md-radio-group>
                </div>
            </div>
        </fieldset>

    </form>
    <div layout-gt-sm="row" layout-align="center">
        <md-button class="md-raised md-primary" ng-click="Step1Ctrl.addChild()">Add Child</md-button>
    </div>

</md-content>
</div>

javascript

         var form =  angular . module ( 'myform'[ 'ngMaterial','     ngAnimate''ngMessages','ngRoute']);  

      form.directive('myStep1', function () {return { templateUrl: 
  'step1-form.html'};});
      form.controller('Step1Ctrl', function ($scope) {
$scope.childern = [];

$scope.addChild = function () {
    $scope.childern.push({});
};
$scope.removeChild = function () {

};
  });

【问题讨论】:

    标签: javascript angularjs angular-material


    【解决方案1】:

    ng-click 表达式作为控制器的方法就不用提了(_不像Controller.methodName() 只是像methodName() 那样表达)

      angular
        .module('MyApp', ['ngMaterial', 'ngMessages'])
        .controller('Step1Ctrl', function($scope) {
          $scope.childern = [{}];
    
          $scope.addChild = function() {
            $scope.childern.push({});
          };
          $scope.removeChild = function() {
    
          };
        });
    
    <div ng-controller="Step1Ctrl" layout="column" ng-cloak="" class="autocompletedemoBasicUsage" ng-app="MyApp">
      <md-content class="md-padding">
        <form name="Form">
    
          <fieldset layout-gt-sm="row" layout-align="center" data-ng-repeat="child in childern">
    
            <md-input-container class="md-block">
              <label>First Name</label>
              <input required name="firstName" ng-model="child.firstName">
              <div ng-messages="Form.firstName.$error">
                <div ng-message="required">First Name is required.</div>
              </div>
            </md-input-container>
            <md-input-container class="md-block" style="max-width:60px;">
              <label>MI</label>
              <input required name=" middleName " ng-model="child.middleName ">
              <div ng-messages="Form.middleName.$error ">
                <div ng-message="required ">Middle Name is required.</div>
              </div>
            </md-input-container>
            <md-input-container class="md-block ">
              <label>Last Name</label>
              <input required name="lastName " ng-model="child.lastName ">
              <div ng-messages="Form.lastName.$error ">
                <div ng-message="required ">Last Name is required.</div>
              </div>
            </md-input-container>
    
            <div class="cell ">
              <label> <b>Student?</b>
              </label>
              <div class="box">
                <md-radio-group ng-model="child.IsStudent">
                  <md-radio-button value="Yes">Yes</md-radio-button>
                  <md-radio-button value="No">No</md-radio-button>
                </md-radio-group>
              </div>
            </div>
    
            <div class="cell">
              <label><b> Child's situation? </b>
              </label>
              <div class="box">
                <md-radio-group ng-model="child.situation">
                  <md-radio-button value="Foster Child">Foster Child</md-radio-button>
                  <md-radio-button value="HMR">Homeless, Migrant, Runaway</md-radio-button>
                </md-radio-group>
              </div>
            </div>
          </fieldset>
    
        </form>
        <div layout-gt-sm="row" layout-align="center">
          <md-button class="md-raised md-primary" ng-click="addChild()">Add Child</md-button>
        </div>
      </md-content>
    </div>
    

    Codepen here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多