在我的应用程序中,我有嵌套表单,一些具有固定名称,一些具有使用 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-if 内的 ng-repeat $index 插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841605/