首页 > 解决方案 > 如何在 Angular 6 中显示表单控件值?

问题描述

我试图在跨度标签中显示表单控件,而不是在输入标签中。所以我尝试了下面的

<div formArrayName="genes">
    <table>
        <thead>
            <tr>
                <th>Position</th>
            </tr>
        </thead>
        <tbody *ngFor="let gene of genesControls.controls; let i = index" [formGroupName]="i">
            <td>
                <span>
                {{gene.position}} //not working
                {{position}} //not working
                {{gene.position.value}} //not working
                {{position.value}} //not working
                </span>

                <!--input formControlName="position" id="pos"> This is working-->
            </td>
        </tbody>
    </table>
</div>

当我在输入标签内尝试时,它正在工作,但跨度或其他标签不起作用,我不知道这里发生了什么,根据文档,我也尝试了值,但出了点问题。我该如何解决这个问题?

标签: angular

解决方案


如果要访问它们的值,可以通过以下方式访问:

     <tbody *ngFor="let gene of genesControls.controls; let i = index" [formGroupName]="i">
        <td>
            <span>{{gene.value.position}}</span>

            <!--input formControlName="position" id="pos"> This is working-->
        </td>
    </tbody>

推荐阅读