2017-07-06 26 views
0

我試圖將值打包到初始化FormGroup中,一旦組件加載就像這樣(其中editForm是FormGroup,editInfo是包含我想要修補到表單中的信息的JSON):Angular2 patchValue在遍歷FormGroup控件時

Object.keys(this.editForm.controls).forEach(key => { 
       if ((key.includes("date") || key.includes("Date")) && this.editInfo[key] != undefined) { 
        this.editForm.patchValue({ 
         key: { 
          date: { 
           year: this.editInfo[key].getFullYear(), 
           month: this.editInfo[key].getMonth() + 1, 
           day: this.editInfo[key].getDate() 
          } 
         } 
        }); 
       } 
      }); 

由於某些原因,這不會修補任何值。當if語句應該運行時,所有的.getFullYear(),.getMonth()和.getDate()值(所有這些鍵值都是Date對象)都被定義並正確。當我更換

this.editForm.patchValue({ 
          key: { 
           date: { 

this.editForm.patchValue({ 
          'specificKey': { 
           date: { 

該鍵是否正確修補。有沒有任何理由不指定密鑰會導致這不起作用? This是我用於我的日期選擇器(我想要修補的值)的模塊,如果這有什麼用處的話。

+0

看看這個https://stackoverflow.com/a/29077216/2983101 –

+0

希望,你會得到你的答案 –

回答