首页 文章

保留由于ngIf而隐藏的组件的引用

提问于
浏览
2

我有一个问题div,它使用ngFor生成一些子组件 . 这些子组件具有数据的输入字段 . 当用户在这些字段中输入数据并单击下一个按钮时,将使用ngIf隐藏问题div并显示预览div . 当再次按下后退按钮预览div隐藏ngIf并出现问题div . 但是因为问题div中的子组件再次被创建,所以来自输入字段的数据消失了 . 以下是我的HTML

<!-- questions

     div-->
    <div class="questions activity_screen" *ngIf="showquestions">
                <div *ngFor="let component of components;let i=index">
                    <app-termhost #cmp [term] = component [title]="component.title" [Qnumber]="i+1"></app-termhost>
                </div>
                <a class="submit_button" (click)="getdata()">Submit</a>
            </div>
        <!-- preview div-->
            <div style="width: 100%;height: 100%;display: flex;align-content: center;justify-content: center" class="preivew" *ngIf="showpdf">
                <button (click)="goback()">go back</button>
            </div>

如何保持这些组件的先前条件,以便在按下后退按钮后再次出现问题div后数据不会丢失!

1 回答

  • 3

    尝试使用隐藏属性而不是* ngIf,隐藏将仅与css一起播放但* ngIf将完全删除元素:

    [hidden]="condition"
    

相关问题