gpt4 book ai didi

angularjs - 根据变量选项的数量引用 ng-repeat 以 Angular 创建的输入

转载 作者:行者123 更新时间:2023-12-02 21:35:02 24 4
gpt4 key购买 nike

正如代码中所述,我面临的问题是我似乎无法找出如何将第二个复选框(使用 ng-repeat 创建)绑定(bind)到某个模型,然后我就可以使用该模型进一步深入代码(显示/隐藏另一组选项)。此外,我还设法使用 $scope.getItterator 根据 $scope.availableOptions 中的 count 参数显示了额外的输入数量函数,但现在我不知道如何访问这些复选框的值?我知道我必须以某种方式使用选项“ng-model”,但我没有这样做,所以任何帮助表示赞赏。

我的代码是here ,但我也在这里展示它:

html:

<div ng-controller='MyCtrl'>
Show additional options <input type="checkbox" ng-model="additionalOptions">
<div ng-show="additionalOptions">
<ul>
<li ng-repeat="option in availableOptions">
<label class="checkbox" for="opt_{{option.id}}">
<input type="checkbox" name="opt_{{option.id}}" id="opt_{{option.}}" />
{{option.name}}

<ul ng-show="if the upper checkbox is clicked">
<li>
<input type="text" ng-repeat="i in getItterator(option.count)" ng-model="SOME_VALUE_TO_BE_USED_BELOW"/>
Output the value based on what's entered above in the textboxes (SOME_VALUE_TO_BE_USED_BELOW)
</li>
</ul>
</label>
</li>
</ul>
</div>
</div>

和我的js:

function MyCtrl($scope) {
$scope.availableOptions = [{"id":"1","name":"easy","count":"2"},{"id":"2","name":"medium","count":"3"},{"id":"3","name":"hard","count":"2"}];

$scope.getItterator=function(n){
var a = new Array();

for(var i=1; i <= n; i++)
a.push(i);

return a;
};

}

最佳答案

尝试ng-model="option.checked":

<input type="checkbox" name="opt_{{option.id}}" id="opt_{{option.}}" ng-model="option.checked"/>

然后您可以像这样访问该属性:

<ul ng-show="option.checked">

DEMO

如果您还需要引用文本框输入。您在该选项上创建一个新属性 (values)。像这样:

{"id":"1","name":"easy","count":"2",values:[]}

HTML:

<div ng-repeat="i in getItterator(option.count)">
<input type="text" ng-model="option.values[$index]"/>
{{option.values[$index]}}
</div>

DEMO

关于angularjs - 根据变量选项的数量引用 ng-repeat 以 Angular 创建的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21605264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com