简体   繁体   English

如何以反应形式动态设置默认值?

[英]How to dynamically set the default value in Reactive form?

I am creating the table with the looping, and if the loop value matches with the particular character I want to set the Reactive Form default value of Repeat else I want to set the empty value in the Reactive Form. 我正在使用循环创建表,并且如果循环值与特定字符匹配,我想将“反应形式”的默认值设置为“ Repeat否则,我想在“反应形式”中设置一个空值。 Following Is my code 以下是我的代码

typescript 打字稿

rDefault:string = "";
create(){
    let form = this.fb.group({
    rp:[this.rDefault]});
    return form;
}

template 模板

<tbody  *ngFor="let c of ch.vl; let i = index" [formGroupName]="i">
    <tr class="ui-widget-content ui-datatable">
        <td>
        {{rpm[ofs+i].label}}
        </td>
        <td>
            <p-dropdown formControlName="rp" [options]="rpm[ofs+i].label =='REPEAT'? compOpt : []" appendTo="body" [disabled]='rpm[ofs+i].label == "REPEAT"?false:true'></p-dropdown>  
        </td>
    </tr>
</tbody>

If {{rpm[ofs+i].label}} this value is equal to "Repeat" I want to set the default form values as "Repeat", else empty value. 如果{{rpm[ofs+i].label}}等于“重复”,我想将默认表单值设置为“重复”,否则为空值。 How can I achieve this? 我该如何实现?

Got the solution for my question but I don't know this is the correct way or not? 得到了我的问题的解决方案,但是我不知道这是正确的方法吗?

In the every loop I am calling the another method from [option] and setting the value in that method. 在每个循环中,我从[option]调用另一个方法,并在该方法中设置值。

template file

    <td>
        <p-dropdown formControlName="rp" [options]="changeDefault(rpm[ofs+i].label)" appendTo="body" [disabled]='rpm[ofs+i].label == "REPEAT"?false:true'></p-dropdown>  
    </td>

Updated TS file

rDefault:string = "";

//this method will change value of rDefault
changeDefault(val){
    this.rDefault = val == "REPEAT" ? "REPEAT" : "";
    let rtnar = 'REPEAT'? this.compOpt : [];
    return rtnar;
}
//


create(){
    let form = this.fb.group({
    rp:[this.rDefault]});
    return form;
}

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

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