【问题标题】:show input form control on initial page load for the default radio button selected in angular 2在初始页面加载时显示在角度 2 中选择的默认单选按钮的输入表单控件
【发布时间】:2017-03-25 14:42:03
【问题描述】:

我有一个页面,我在其中显示基于单选按钮选择的文本框控件。当我从一个单选按钮切换到另一个单选按钮时,我能够显示文本框。在我的代码中,我默认选择了单选按钮“男性”,如何在页面初始加载时默认显示第一个输入控件。

HTML 页面

<div class="radio">
            <label>
                <input type="radio" name="gender" value="Male" [formControl]="complexForm.controls['gender']" (click)="setradio('Male')" checked="checked">
                Male
            </label>
        </div>

        <div class="radio">
            <label>
                <input type="radio" name="gender" value="Female" [formControl]="complexForm.controls['gender']" (click)="setradio('Female')">
                Female
            </label>
        </div>

        <!--<div *ngIf="edited">-->


            <div *ngIf="isSelected('Male')" >
                <input type="text"/>test   
            </div>

            <div *ngIf="isSelected('Female')">
                <input type="text"/> test2
            </div>

组件类:

private selectedLink: string;
setradio(e: string): void {

    this.selectedLink = e;        
}

isSelected(name: string): boolean {

    if (!this.selectedLink) { 
        return false;
    }

    return (this.selectedLink === name); 
}

我尝试将*ngIf="isSelected('Male')=='true' 的默认值设置为true,但没有成功。有没有其他方法可以使输入控件在页面加载时显示。

【问题讨论】:

    标签: angular


    【解决方案1】:
    private selectedLink: string = 'Male';
    

    当您第一次加载模板时,第一个控件被选中。如果您最初设置selectedLink='Male',同样的事情将显示第一个输入。

    【讨论】:

    • 谢谢@Roman,它就像魅力一样。虽然需要将文本从“邮件”编辑为“男性”:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2021-10-07
    • 2015-09-04
    • 2015-08-27
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多