ng-repeat$index 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">

最新更新