ng-repeat $index 内插 ng-if

ng-repeat $index interpolation inside ng-if

在我的应用程序中,我有嵌套表单,一些具有固定名称,一些具有使用 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] 完全相同的 属性。

尝试:

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