简体   繁体   English

如何使用ng-for ngif来访问ng-container中的变量?

[英]How do I access a variable inside a ng-container with an ng-for for my ngif?

I am using a template-driven form. 我使用的是模板驱动的表单。 I have an ng-container with an ng-for that create radio buttons. 我有一个带有ng-for的ng-container,用于创建单选按钮。 As part of my validation, I have a variable name that checks for errors. 作为验证的一部分,我有一个变量名称来检查错误。

Below is a snippet of my code: 以下是我的代码片段:

    <div class="col-md-4">
        <ng-container *ngFor="let item of assetFormatOptions | wfsort">
            <ng-container *ngIf="!item['isHidden']">
                <div class="form-group form-group--inline">
                    <label class="radio">


<!--Created variable called assetFormat for validation purposes-->
                        <input type="radio" #assetFormat="ngModel" name="DE:Asset Format" value="{{item.value}}" ngModel required>
                        <span class="radio__input"></span>
                        <span class="radio__label hidden-xs">{{item.label}}</span>
                    </label>
                </div>
            </ng-container>
        </ng-container>


<!--trying to access variable above-->
        <div *ngIf="submitted && assetFormat.errors && (assetFormat.touched || submitted && !assetFormat.valid)" class="alert alert--danger">
            <span class="error">This field is required.  
                <span class="icon-chevron-up"></span>
           </span>
        </div>
    </div>

This goal is for the ngIf at the bottom to read the status of the radio buttons. 此目标是针对底部的ngIf读取单选按钮的状态。 In this case, there would be 3 or 4 different radio buttons. 在这种情况下,将有3或4个不同的单选按钮。

As of right now, the ngIf is not able to read the radio button variables. 截至目前,ngIf无法读取单选按钮变量。 How do I get the radio button variable to my ngIf? 如何将单选按钮变量输入到ngIf? I don't want the ngIf inside my for loop. 我不想在我的for循环中使用ngIf。

Pass your variable as context to template. 将变量作为上下文传递给模板。 try this 尝试这个

<div class="col-md-4">
        <ng-container *ngFor="let item of assetFormatOptions | wfsort">
           <ng-container *ngTemplateOutlet="itemContainer; context:{item:item}"></ng-container>
        </ng-container>


<!--trying to access variable above-->
        <div *ngIf="submitted && assetFormat.errors && (assetFormat.touched || submitted && !assetFormat.valid)" class="alert alert--danger">
            <span class="error">This field is required.  
                <span class="icon-chevron-up"></span>
           </span>
        </div>
    </div>

 <ng-template #itemContainer let-item="item" *ngIf="!item['isHidden']">
                <div class="form-group form-group--inline">
                    <label class="radio">


<!--Created variable called assetFormat for validation purposes-->
                        <input type="radio" #assetFormat="ngModel" name="DE:Asset Format" value="{{item.value}}" ngModel required>
                        <span class="radio__input"></span>
                        <span class="radio__label hidden-xs">{{item.label}}</span>
                    </label>
                </div>
            </ng-template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM