简体   繁体   English

AngularJS动态表单

[英]AngularJS Dynamic Form

I have a function for Dynamic form like this 我有一个像这样的动态表格功能

$scope.ProdLinkFormData = {};
$scope.ProdLinkFormData.link = [{
  link: null,
  price: null,
  priceType: null,
  category: null
}, {
  link: null,
  price: null,
  priceType: null,
  category: null
}];
$scope.addLinkItem = function() {
  $scope.ProdLinkFormData.link.push({
    link: null,
    price: null,
    priceType: null,
    category: null
  });
};

$scope.removeLinkItem = function(linkItem) {
  //console.log(subscriber);
  $scope.ProdLinkFormData.link.pop(linkItem);
};

My view contains something like this 我的观点包含这样的内容

<div id="web" ng-repeat="linkItem in ProdLinkFormData.link">
  <div class="row col-xs-12">
    <div class="col-xs-6">
      <div class="form-group">
        <label class="col-md-6 control-label">*Category: </label>
        <select class="pull-left" id="prodLinkCategory" name="prodLinkCategory" ng-required="true" ng-model="prodLinkNew[$index].category" required/>
        <option ng-repeat="item in scpProdLinkCat" value="{{item.prodlink_category_value}}" ng-selected="prodLinkNew[$index].category==item.prodlink_category_value">{{item.prodlink_category_name}}</option>
        </select>
      </div>
      <div class="form-group">
        <label class="col-md-6 control-label">Price (optional): </label>
        <div class="input-group">
          <div class="input-group-btn">
            <select class="btn btn-primary dropdown-toggle dropdown-toggle-split" name="price" id="price" ng-model="prodLinkNew[$index].price_type">
              <option ng-repeat="item in scpPriceType" value="{{item.pricetype_name}}">{{item.pricetype_name}}</option>
            </select>
          </div>
          <input type="number" class="col-md-1 input-group inline form-control" name="price" id="price" ng-model="prodLinkNew[$index].price" ng-pattern="/^[0-9]+$/" / ng-value="prodLinkNew[$index].price">
        </div>
      </div>
    </div>
    <div class="col-xs-6">
      <div class="form-group">
        <label class="col-md-6 control-label">*Product Link EC: </label>
        <div class="col-md-6">
          <input type="text" class="form-control" name="prodEC" ng-required="true" id="prodEC" ng-model="prodLinkNew[$index].Productlink_EC" required/>

        </div>
      </div>
    </div>
  </div>

It displays accordingly but my problem is that if I have two data on the database, only one data will be displayed on the form on page load and the second one will be displayed only after I add another from. 它会相应显示,但是我的问题是,如果我在数据库中有两个数据,则页面加载时表单上将仅显示一个数据,而仅当我添加另一个数据后,才会显示第二个数据。 How can I display the two data simultaneously? 如何同时显示两个数据?

 angular.module('mainApp', []) .controller('MainCtrl', function($scope){ $scope.ProdLinkFormData = {}; $scope.ProdLinkFormData.link = [ { link: null, price: null, priceType: null, category: null }, { link: null, price: null, priceType: null, category: null } ]; $scope.addLinkItem = function() { $scope.ProdLinkFormData.link.push({ link: null, price: null, priceType: null, category: null }); }; $scope.removeLinkItem = function(linkItem) { //console.log(subscriber); $scope.ProdLinkFormData.link.pop(linkItem); }; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.js"></script> <div ng-app="mainApp" ng-controller="MainCtrl"> <div id="web" ng-repeat="linkItem in ProdLinkFormData.link"> <div class="row col-xs-12"> <div class="col-xs-6"> <br/><br/> <div class="form-group"> <label class="col-md-6 control-label">*Category: </label> <select class="pull-left" id="prodLinkCategory" name="prodLinkCategory" ng-required="true" ng-model="prodLinkNew[$index].category" required/> <option ng-repeat="item in scpProdLinkCat" value="{{item.prodlink_category_value}}" ng-selected="prodLinkNew[$index].category==item.prodlink_category_value">{{item.prodlink_category_name}}</option> </select> </div> <br/><br/><br/> <div class="form-group"> <label class="col-md-6 control-label">Price (optional): </label> <div class="input-group"> <div class="input-group-btn"> <select class="btn btn-primary dropdown-toggle dropdown-toggle-split" name="price" id="price" ng-model="prodLinkNew[$index].price_type"> <option ng-repeat="item in scpPriceType" value="{{item.pricetype_name}}">{{item.pricetype_name}}</option> </select> </div> <input type="number" class="col-md-1 input-group inline form-control" name="price" id="price" ng-model="prodLinkNew[$index].price" ng-pattern="/^[0-9]+$/"/ ng-value="prodLinkNew[$index].price"> </div> </div> <br/><br/> </div> <div class="col-xs-6"> <br/><br/> <div class="form-group"> <label class="col-md-6 control-label">*Product Link EC: </label> <div class="col-md-6"> <input type="text" class="form-control" name="prodEC" ng-required="true" id="prodEC" ng-model="prodLinkNew[$index].Productlink_EC" required/> </div> </div> </div> <br/><br/> </div> <br/><br/> </div> </div> </div> 

Just copied and pasted your code and added a module around it and it loads two entries into the view for the ng-repeat as expected. 只需复制并粘贴您的代码并在其周围添加一个模块,即可按预期将两个条目加载到ng-repeat的视图中。

Verify your data coming back from the server paste it here if you'd like for what gets populated into the link property array. 如果希望将填充到链接属性数组中的内容,请验证从服务器返回的数据将其粘贴到此处。

If i understand your question 如果我理解你的问题

  1. at first you have a new array from api for example has 2 object 首先,您有一个来自api的新数组,例如有2个对象
  2. 2nd you want to show first form 第二个要显示的第一个表格
  3. when you click on add, should added the 2nd form 当您单击添加时,应添加第二个表格
  4. this example is dynamic you can add n object in apiArray 这个例子是动态的,你可以在apiArray添加n对象

 'use strict'; var app = angular.module('app', []); app.controller("ctrl", function ($scope) { $scope.apiArray = [ { num: 1, }, { num: 2, }]; //create new model $scope.newArray = []; $scope.newArray.push($scope.apiArray[0]); $scope.add = function () { var apiArrayLength = $scope.apiArray.length - 1; var newArrLength = $scope.newArray.length - 1; if (newArrLength < apiArrayLength) { $scope.newArray.push($scope.apiArray[newArrLength + 1]) } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div ng-app="app" ng-controller="ctrl"> <div class="col-lg-4 col-lg-offset-4"> <br> <br> <div class="alert alert-info"> <b>apiArray</b> length is {{apiArray.length}}, we can add just {{apiArray.length}} form in <b>newArray</b>, form 1 is default added to newArray </div> <button ng-click="add()" class="btn btn-primary">add</button> <div class="clearfix"></div> <hr> <div class="col-lg-12" ng-repeat="linkItem in newArray"> <div class="well"> form {{linkItem.num}} </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM