简体   繁体   English

使用angularjs,如何基于同一表单中另一个输入标签的状态将输入标签动态添加到表单

[英]using angularjs, how do I dynamically add an input tag to a form based on the state of another input tag in the same form

I am constructing a form in Angularjs. 我正在Angularjs中构造一个表单。 Based on the value of a radio button in the form, I want to change the number of options in another set of radio buttons. 根据表单中单选按钮的值,我想更改另一组单选按钮中的选项数量。 This is very easy using direct DOM manipulation, but I cannot see how to do it in angular. 使用直接DOM操作非常容易,但是我看不到如何做到有角度。

I imagine that the solution lies in making the second set of radio buttons a directive, but I don't see how to communicate the state of the first set of radio buttons to the directive. 我想解决方案在于将第二组单选按钮设置为指令,但是我看不到如何将第一组单选按钮的状态传达给指令。

Here is a fiddle that may have what you're looking for. 这是一个小提琴 ,可能有您想要的东西。 If not I can modify it a bit. 如果没有,我可以修改一下。

In yourfile.html : yourfile.html

<div ng-controller="MyCtrl">
  Hello, how many radio buttons!
  <br />

  <form name="form1">

  <label>
    <input type="radio" ng-model="optionCount" ng-change="optionsUpdated()" ng-value="0">0
  </label>
  <br/>
  <label>
    <input type="radio" ng-model="optionCount" ng-change="optionsUpdated()" value="1">1
  </label>
  <br/>
  <label>
    <input type="radio" ng-model="optionCount" ng-change="optionsUpdated()" value="2">2
  </label>
  <br/>
  <label>
     <input type="radio" ng-model="optionCount" ngchange="optionsUpdated()" value="3">3
  </label>
  <br/>
  </form>
  <hr> 
  There should be {{optionCount}} radio buttons!
  <br />
  <form name="form2">
    <div ng-repeat="radio in radioArray">
      <label>
      <input type="radio" ng-model="myOptions" value="1">Option {{$index + 1}}
      </label>
     <br/>
    </div>
  </form>
 </div>

And in yourfile.js : yourfile.js

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.numOpts = [0, 1, 2, 3, 4]
  $scope.optionCount = 0;
  $scope.optionsUpdated = function() {
    var optionNum = Number($scope.optionCount)
    $scope.radioArray = new Array(optionNum);
  }
}

Its easy to make the value of the checkbox controll the input field just create a scope that the two will communicate [can use the default] 只需将复选框的值控制在输入字段即可,只需创建一个将两者通信的范围即可[可以使用默认值]

use $scope.modelCheckbox for the checkbox and $scope.modelInput for the input and set the input disabled on the value change in $scope.modelCheckbox $ scope.modelCheckbox用作复选框,将$ scope.modelInput用作输入,并在$ scope.modelCheckbox的值更改后将输入设置为禁用

Its typical angular use using model binding so you will get more here 它通常使用模型绑定进行角度使用,因此您将在此处获得更多

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

相关问题 根据其他输入以相同形式填充标签输入 - Populating a tag input based on other inputs in same form 通过vanilla javascript提交表单后,如何删除用户在输入标签中输入的输入? - How do I remove the input typed in by user in an input tag after form submission through vanilla javascript? 如何使用jquery基于选择标签的输出向输入标签动态添加REQUIRED属性? - how to dynamically add REQUIRED attribute to input tags based on output of select tag using jquery? 如何使用jQuery动态添加链接到复选框或输入标签中的值 - How to dynamically add values linked to a checkbox or in the input tag using jQuery javascript 如何使用按钮动态添加元素到表单标签 - javascript how to add element dynamically to form tag using button 我如何在动态插入时初始化jQuery datepicker <input> 标签? - How do I initialize a jQuery datepicker on a dynamically inserted <input> tag? 使用JQuery为DOM动态地为每个输入标签添加标签标签 - Add a label tag for each input tag dynamically for the DOM using JQuery 如何使用Jquery基于选择输入以表单形式动态添加DIV - How to add DIV's dynamically in a form based on a select input with Jquery 如何从表单输入动态设置状态 - How to dynamically set State from Form input 如何获取HTML表单的输入标签ID? - How to get input tag id of html form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM