Bun*_*nut 3 angular2-forms angular
我在Angular 2中创建了一个表单,用户可以使用该表单编辑SearchProfile可以从列表中选择的表单。最初,一切正常,但是当我从SearchProfiles 列表中选择其他项目时,会收到以下异常:
There is no FormControl instance attached to form control element with name: controlName。整个事情由3个部分组成:2层的组件SelectProfileComponent,SearchProfileComponent以及服务ProfileService。ProfileService包含ActiveProfile已选择SearchProfile进行编辑的。
ProfileService看起来像这样:
@Injectable()
export class ProfileService {
private activeProfileSource = new ReplaySubject<ISearchProfile>();
activeProfile$ = this.activeProfileSource.asObservable();
constructor(private http: Http) {
this.selectProfile();
}
public getSearchProfile(profileId: number = null): Observable<ISearchProfile> {
let url = `<url>?profileid=${profileId}`;
this.http.get(url).map((result) => {
return <ISearchProfile>.result.json();
});
}
public selectProfile(profileId: number = null) {
this.getSearchProfile(profileId).subscribe((p) => {
this.activeProfileSource.next(p);
});
}
}
Run Code Online (Sandbox Code Playgroud)
SelectProfileComponent包含带有配置文件的列表,该列表change在SearchProfile选择a 时触发-event 。
<select (change)="setActiveProfile($event.target.value)">
<option *ngFor="let profile of profiles" [value]="profile.Id" [innerHtml]="profile.name"></option>
</select>
export class ProfileSelectionComponent implements OnInit {
private profiles: ISearchProfile[] = null;
constructor(private profileService: ProfileService) {}
//get profiles logic
public setActiveProfile(profileId: number): void {
this.profileService.selectProfile(profileId);
}
}
Run Code Online (Sandbox Code Playgroud)
该SearchProfileComponent具有Subscription的activeProfile,并应表现出的特性activeProfile,使用户可以对其进行编辑。
SearchProfileComponent看起来像这样:
<form [formGroup]="profileForm" *ngIf="!isLoading">
<input type="text" name="name" formControlName="name" [(ngModel)]="searchProfile.name" />
</form>
export class SearchProfileComponent implements OnInit {
private isLoading: boolean = true;
private activeProfileSubscription: Subscription;
private searchProfile: ISearchProfile = null;
public profileForm: FormGroup;
constructor(private profilService: ProfileService
private formBuilder: FormBuilder) { }
ngOnInit() {
this.activeProfileSubscription = this.profileService.activeProfile$.subscribe((profile: ISearchProfile) => {
this.searchProfile = profile;
this.createForm();
this.isLoading = false;
});
}
private createForm() {
this.profileForm = this.formBuilder.group({
["name"]: [this.searchProfile.name, null]
});
}
}
Run Code Online (Sandbox Code Playgroud)
Kuz*_*bko 11
我以这种方式解决了这个问题:
之前:
formControlName="description"
Run Code Online (Sandbox Code Playgroud)
后:
[formControl]="form.controls['description']"
Run Code Online (Sandbox Code Playgroud)
我通过在组件构造函数中初始化表单一次解决了这个问题,即
constructor() {
this.form = this.initForm();
}
Run Code Online (Sandbox Code Playgroud)
或者
ngOnInit() {
this.form = this.initForm();
}
Run Code Online (Sandbox Code Playgroud)
并在没有构造函数的情况下在组件中的任何位置删除组件中表单的重新初始化,即
this.form = this.initForm();
Run Code Online (Sandbox Code Playgroud)
你的创建FormGroup有点错误。您不需要将控件名称括在方括号和双引号中。在官方 angular 文档上阅读更多关于反应形式的信息。
从此更改您对 FormGroup 的创建:
this.profileForm = this.formBuilder.group({
["name"]: [this.searchProfile.name, null]
});
Run Code Online (Sandbox Code Playgroud)
对此:
this.profileForm = this.formBuilder.group({
name: [this.searchProfile.name]
});
Run Code Online (Sandbox Code Playgroud)
你应该在你的 html 中做一些改变。提示:如果您使用的是反应式表单,则不需要[(ngModel)]输入,请将其删除。这是您输入的正确 html:
<form [formGroup]="profileForm" *ngIf="!isLoading">
<input type="text" name="name" formControlName="name" />
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4310 次 |
| 最近记录: |