我对Angular 2 Form(formbuilder)的默认值有问题:
我的默认值是observables(我正在从服务器检索),所以我不能像这样实现它们:
我的默认值是observables(我正在从服务器检索),所以我不能像这样实现它们:
export class UserComponent implements OnInit{ userForm: ControlGroup; userData: any; // Initialise the observable var ngOnInit():any { this.userData = this._dataService.getAllData() // My Observable .subscribe( data => { this.userData = data; } ); this.userForm = this._formBuilder.group({ // below the default value 'username': [this.userData.username,Validators.compose([ this.usernameValid ])] }
我会尝试这个,因为数据是异步加载的.因此,您需要在响应/接收时更新表单元素的值.
ngOnInit():any { this.userData = this._dataService.getAllData() .subscribe( data => { this.userData = data; this.userForm.controls.username.updateValue( this.userData.username); } ); this.userForm = this._formBuilder.group({ 'username': [this.userData.username,Validators.compose([ this.usernameValid ])]; }
原文地址:https://www.jb51.cc/angularjs/141241.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。