用空值和填充FormGroup
Populate FormGroup with null value and
Chrome控制台出现错误:EXCEPTION: error: Uncaught (in promise): TypeError: Cannot read property 'applicationName' of null.
模型:导出类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 });
}
}
我想要实现的:
- 当我构建我的代码,我希望我的formGroup应该填充null值。
- 当我填写数据并将其保存到我的服务中的行为主题时,当我重新访问页面时,我的formGroup应该与数据服务同步。
通过添加:
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));
}
相关文章:
- 如何设置html元素填充的动画
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何通过ajax刷新JSF填充的javascript变量
- 用与线条相同的颜色填充多折线图上的点
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 用我的json数据填充JQuery DataTable
- 使用 jQuery 检查所有值是否为空或已填充
- 为什么不't我的变量在我的控制器中填充后在我的视图中呈现
- 如何让typeahead在我的搜索栏中填充自定义参数
- 如何使用JSON字符串中的jQuery填充下拉框
- 动态填充两个下拉菜单
- 如何将CSS(特别是填充/边距)应用于select下拉菜单的选项或optgroup
- spine.js在启动时填充模型实例
- 如何将要单独填充的每个HTML画布路径/形状分开
- CSS赢得't填充视图端口
- 从JSON填充列表(而不是HTML)
- Fabric.js-更改矩形填充
- 使用JSON文件中的变量(字符串)填充文本区域
- 用空值和填充FormGroup