gpt4 book ai didi

javascript - ng-repeat $index 内插 ng-if

转载 作者:行者123 更新时间:2023-11-27 23:19:10 26 4
gpt4 key购买 nike

在我的应用程序中,我有嵌套表单,一些具有固定名称,一些具有使用 ng-repeat 索引生成的名称:

<form name="rootForm"> 
<div ng-repeat="child in childForms">
<ng-form name="childForm_{{$index}}">
<!-- form content-->
</ng-form>
</div>
<ng-form name="fixedName">
<!-- form content-->
</ng-form>
<ng-form name="anotherFixedName">
<!-- form content-->
</ng-form>
</form>

在同一个 html 文件中,我想通过 ng-if 语句访问这些​​表单的 $valid 属性。那可能吗?我正在尝试:

<div>
<div ng-repeat="child in childForms">
<div ng-if="rootForm.childForm_[$index].$valid">
Child form {{index}} is valid!
</div>
</div>
<div ng-if="rootForm.fixedName.$valid"> Valid! </div>
<div ng-if="rootForm.anotherFixedName.$valid"> Valid! </div>
</div>

而且它适用于具有固定名称的表单。但对于具有生成名称的子表单,它不会。我做错了什么?

最佳答案

您使用 ng-if 的重复元素使用的表达式不会如您所愿。

代替:

<div ng-if="rootForm.childForm_[$index].$valid">

大概应该是:

<div ng-if="rootForm['childForm_' + $index].$valid">

在前者中,标记会尝试访问与 childForm_[$index] 完全相同的属性。

关于javascript - ng-repeat $index 内插 ng-if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841605/

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