[英]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.