在ng-if内重复$index插值。

时间:2022-12-02 16:50:29

In my app I have nested forms, some with fixed names and some with generated names using ng-repeat index:

在我的app中,我有嵌套的表单,有固定名字的,也有使用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>

In the same html file, I want to access those forms $valid property through ng-if statement. Is that possible? I'm trying with:

在同一个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>

And it works fine with forms with fixed names. But for child forms with generated names it does not. What am I doing wrong?

它对名字固定的表单也适用。但是对于生成名称的子表单则不是这样。我做错了什么?

2 个解决方案

#1


1  

Your repeated element with ng-if uses an expression which won't do as you'd expect.

使用ng-if的重复元素使用的表达式不会像您预期的那样。

Instead of:

而不是:

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

It should probably be:

它应该是:

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

In the former, the markup would've been trying to access a property named exactly as childForm_[$index].

在前者中,标记将尝试访问名为childForm_[$index]的属性。

#2


1  

Try:

试一试:

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

#1


1  

Your repeated element with ng-if uses an expression which won't do as you'd expect.

使用ng-if的重复元素使用的表达式不会像您预期的那样。

Instead of:

而不是:

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

It should probably be:

它应该是:

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

In the former, the markup would've been trying to access a property named exactly as childForm_[$index].

在前者中,标记将尝试访问名为childForm_[$index]的属性。

#2


1  

Try:

试一试:

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