2016-12-06 122 views
0

我想獲得的id在此基礎上在URL角2觀察到的問題

設置工作的附加模式,但得到錯誤編輯

export class GradeComponent implements OnInit { 

    public isNew:boolean=true; 
    public frmGrade: FormGroup; 
    public subscription:any; 
    public oldGrade:Grade; 

    constructor(
    private formBuilder:FormBuilder , 
    private gradeService:GradeService, 
    private router:Router, 
    private activatedRoute:ActivatedRoute 
) { } 


ngOnInit() { 
    if(typeof this.activatedRoute.snapshot.params['id'] ==='undefined'){ 
     this.frmGrade = this.formBuilder.group({ 
     grade: ['', Validators.required], 
     description: '' 
     }); 
    }else{ 
     this.setForUpdate(); 
    } 
    } 

    private setForUpdate(){ 
     this.isNew=false; 
     this.gradeService 
      .getOneGrade(this.activatedRoute.snapshot.params['id']) 
      .subscribe(
       data => { 
        this.oldGrade = data, 
        this.frmGrade = this.formBuilder.group({ 
         grade: [this.oldGrade.grade, Validators.required], 
         description: this.oldGrade.description 
        }); 
       }, 
       err => console.error(err), 
       () => console.log('done') 
      ); 
    } 

上的數據,但我得到的錯誤 這.formBuilder未定義如何處理這個。

+1

'this.formBuilder'不會出現;-)爲什麼你認爲它應該在那裏? –

+0

更新記錄,我想從ID獲取舊數據,然後分配給frm – Ash

+0

什麼形式? 。 。 ..。 –

回答

1

FormBuilder是一個服務可注入的,只是得到一個Dependancy Injection實例。

在類補充一點:

constructor(private formBuilder: FormBuilder) {} 

,你準備好去。

+0

它已被添加 – Ash

+0

如果您沒有提供我們的整個代碼,我們無法猜測它。在你的問題中完成。 – Sakuto

1
export class GradeComponent implements OnInit { 

    public isNew:boolean=true; 
    public frmGrade: FormGroup; 
    public subscription:any; 
    public oldGrade:Grade; 

    constructor(
    private formBuilder:FormBuilder , 
    private gradeService:GradeService, 
    private router:Router, 
    private activatedRoute:ActivatedRoute 
) { } 


ngOnInit() { 

    this.frmGrade = this.formBuilder.group({ 
     grade: ['', Validators.required], 
     description: '' 
     }); 

    if(typeof this.activatedRoute.snapshot.params['id'] !=='undefined') { 
    this.setForUpdate(); 

    } 
    } 

    private setForUpdate(){ 
     this.isNew=false; 
     this.gradeService 
      .getOneGrade(this.activatedRoute.snapshot.params['id']) 
      .subscribe(
       data => { 
        this.oldGrade = data, 
        this.frmGrade = this.formBuilder.group({ 
         grade: [this.oldGrade.grade, Validators.required], 
         description: this.oldGrade.description 
        }); 
       }, 
       err => console.error(err), 
       () => console.log('done') 
      ); 
    } 
+0

謝謝你正在像冠軍一樣工作! :) – Ash