2017-08-29 35 views
0

有一些博客已經發布了這個錯誤,但沒有一個是爲FormGroup規定,他們都給解決方案與FormArray嵌套FormGroup返回錯誤,而建設的角度應用在生產模式

我不得不做出一個嵌套FormGroup在角度4出於某種目的,當我在開發模式下使用該代碼時,它不會顯示任何問題並完美編譯。但是,當我試圖在生產模式下編譯相同的代碼時,它在「AbstractControl」類型中不存在「屬性」控件引發錯誤。

component.ts:

import { Component, OnInit } from '@angular/core'; 
import { CustomerService } from '../../service/customer.service'; 
import { FormControl,FormGroup,FormBuilder,Validators,FormArray } from '@angular/forms'; 
enter code here 
@Component({ 
selector: 'app-customer', 
templateUrl: './customer.component.html', 
styleUrls: ['./customer.component.css'], 
providers: [CustomerService,GeolocationService] 
}) 
export class CustomerComponent implements OnInit { 
sheduleForm:FormGroup; 
constructor(private service:CustomerService,private geoLocation:GeolocationService,private router:Router,private fb: FormBuilder) { 
this.sheduleForm = fb.group({ 
user_schedule_details:fb.group({  
    mon:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    tue:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    wed:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    thu:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    fri:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    sat:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }), 
    sun:fb.group({ 
     schedule_time:['00:00:00'], 
     status:[false] 
    }) 
    }),  
    time_zone: [d.getTimezoneOffset()], 
    time_format : [false], 
    city:[null], 
    state:[null], 
}); 
} 

component.html:

<form [formGroup]="sheduleForm" (ngSubmit)="saveShedule(sheduleForm.value)" novalidate> 
<div class="schedule_time">Schedule Time: 
      <span> 
      <md-input-container> 
       <input type="time" mdInput [formControl]="sheduleForm.controls['user_schedule_details'].controls['mon'].controls['schedule_time']"> 
      </md-input-container> 
      </span> 
     </div> 
     </div> 
...... so on..... 

所以我得到的問題在這個網站與[formControl] =「sheduleForm.controls [ 'user_schedule_details']。controls ['mon']。controls ['schedule_time']「 任何人都可以幫助我。我知道這個表單控制名稱看起來很奇怪。但只有這樣我才能夠訪問表單值。如果這不是使用formControl的正確方法,請告訴我,我可以使用什麼呢?

回答

0

嘗試調整你的HTML類似:

<form [formGroup]="sheduleForm" 
     (ngSubmit)="saveShedule(sheduleForm.value)" 
     novalidate> 
    <div formGroupName="user_schedule_details"> 
    <div formGroupName="mon"> 
     <div class="schedule_time">Schedule Time: 
     <span> 
      <md-input-container> 
      <input type="time" 
        mdInput 
        formControlName="schedule_time"/> 
      </md-input-container> 
     </span> 
     </div> 
    </div> 
    ...... so on..... 

另外,如果你想使用[formControl]無論如何,你可以使用get()功能做到這一點:

<md-input-container> 
    <input type="time" 
     mdInput 
     [formControl]="sheduleForm.get('user_schedule_details.mon.schedule_time)"/> 
</md-input-container> 

甚至

[formControl]="sheduleForm.get('user_schedule_details').get('mon').get('schedule_time')" 
+0

非常感謝。這解決了問題。第一個沒有工作,但第二個解決方案工作。[formControl] =「sheduleForm.get('user_schedule_details')。get('mon')。get('schedule_time')」「 –