第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Angular:在FormGroup中定義默認(rèn)值輸入FormField

Angular:在FormGroup中定義默認(rèn)值輸入FormField

瀟湘沐 2022-01-13 15:43:36
從下面的代碼中可以看出,我將默認(rèn)值設(shè)置為文本區(qū)域:<form [formGroup]="entryForm" (ngSubmit)="onSubmit()">     <div class="view">            <div class="col">                        <mat-form-field>                            <textarea matInput value="Test..." formControlName="firstValue"></textarea>                        </mat-form-field>            </div>            <button type="submit">Submit</button>      </div></form>我正在初始化我的 FormGroup 和 FormFields,如下所示:ngOnInit() { this.entryForm = new FormGroup({   firstValue: new FormControl('') });}public onSubmit() {  console.log(this.entryForm.value);}但是當(dāng)我點(diǎn)擊提交時(shí),值firstValue似乎是空的,而不是“測試...”值,就像在 dom 元素中定義的一樣。
查看完整描述

3 回答

?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊

改變這個(gè):

firstValue: new FormControl('')

對此:

firstValue: new FormControl('Test...')

并從文本區(qū)域中刪除屬性值:

<textarea formControlName="firstValue"></textarea>

在這里,您可以找到一個(gè)完整的工作示例(基于答案)

筆記:

這種方法的問題是初始 Textarea 值將是“Test...”,這意味著用戶將看到這樣的文本。


查看完整回答
反對 回復(fù) 2022-01-13
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

模板文件:您formNameControl的不正確:


你可以看看這個(gè)演示可能對你有幫助!


  <form [formGroup]="entryForm" (ngSubmit)="onSubmit()">

         <div class="lang">

                <div class="lang-col">

                      <textarea  formControlName="firstValue">   </textarea>


                </div>

                <button type="submit">Submit</button>

          </div>

    </form>

類文件


entryForm: FormGroup;

  ngOnInit() {

    this.entryForm = new FormGroup({

      firstValue: new FormControl('Test...')

    });

  }


  onSubmit() {

    console.log(this.entryForm.value);

  }

或者您可以檢查是否formControlName為空然后設(shè)置默認(rèn)值


  onSubmit() {


    if(this.entryForm.get('firstValue').value == "") {

      this.entryForm.patchValue({

        firstValue: 'Your defualt value goes here'

      }); 

    }

    console.log(this.entryForm.value);

  }


查看完整回答
反對 回復(fù) 2022-01-13
?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

作業(yè)中的問題。你必須在類定義之后分配你的輸入表單的值,像這樣


export class MyComponent implements OnInit{

    public entryForm: FormGroup = new FormGroup({

        firsValue: new formControl('')

    });

    constructor()

    ngOnInit()

}


查看完整回答
反對 回復(fù) 2022-01-13
  • 3 回答
  • 0 關(guān)注
  • 420 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)