0

Chrome コンソールでエラーを取得しています: 例外: エラー: キャッチされていません (約束されています): TypeError: null のプロパティ 'applicationName' を読み取れません。

モデル: クラス BasicInfoModel のエクスポート {

    applicationName: string;
    localDirectoryPath: string; 
}

親コンポーネントにデータを送信するコントローラー、そこにある親コンポーネントがサービスに保存されます。

コントローラ:

import { Component, Output, OnInit, EventEmitter} from '@angular/core';
import { FormGroup, FormControl, REACTIVE_FORM_DIRECTIVES, Validators,               
FormBuilder, FormArray}from "@angular/forms";
import { Observable } from "rxjs/Rx";
import { BasicInfoModel } from '../basicinfomodel';
import { BasicInfoService} from '../app.dev.basicinfo.service';

@Component({
   selector: 'basic-info',
   templateUrl: './basicInfo.html',
   styleUrls: ['../../ComponentStyles.css'],
   directives: [REACTIVE_FORM_DIRECTIVES]
})

export class BASIC_INFOComponent implements OnInit {

observableBasic: BasicInfoModel;
basicInfoForm: FormGroup;

@Output() basicInfoUpdate = new EventEmitter<JSON>();
@Output() basicInfoFormValid = new EventEmitter<Boolean>();

constructor(private formBuilder: FormBuilder, private BasicInfoService:      
BasicInfoService) {  }

onSubmit() {
    debugger;
    this.observableBasic;
    this.basicInfoUpdate.emit(this.basicInfoForm.value);
}

ngOnInit() {

    this.basicInfoForm = new FormGroup({
        'applicationName': new FormControl('', Validators.required),
        'localDirectoryPath': new FormControl('', Validators.required)
    });

    this.basicInfoForm.valueChanges.subscribe(data => console.log('form   
    changes', data));
    this.BasicInfoService.currentBasicInfo
        .subscribe(
        (basic: BasicInfoModel) => {
            this.observableBasic = basic;
        });

    (<FormGroup>this.basicInfoForm).setValue(this.observableBasic, { onlySelf: true });
}

}

私が達成したいこと:

  1. コードをビルドするときに、formGroup に null 値を設定する必要があります。
  2. データを入力してサービスの behaviourSubject に保存したとき、後でページに再度アクセスしたときに、formGroup がデータ サービスと同期している必要があります。
4

1 に答える 1

1

: (this.observableBasic != undefined) を追加してコントローラーを変更しました。

ngOnInit() {
    this.basicInfoForm = new FormGroup({
        'applicationName': new FormControl('', Validators.required),
        'localDirectoryPath': new FormControl('', Validators.required)
    });

    this.BasicInfoService.currentBasicInfo
        .subscribe((basic: BasicInfoModel) => { this.observableBasic = basic; });

    if (this.observableBasic != undefined) {
        (<FormGroup>this.basicInfoForm).setValue(this.observableBasic, { onlySelf: true });
    }      

    this.basicInfoForm.valueChanges.subscribe(data => console.log('form changes', data));
}
于 2016-10-04T04:11:39.213 に答える