没有 FormControl 实例附加到具有名称的表单控件元素

There is no FormControl instance attached to form control element with name

我在 Angular 2 中创建了一个表单,用户可以使用该表单编辑 SearchProfile 他们可以从列表中 select。最初一切正常,但是当我 select 来自 SearchProfile 列表的不同项目时,我收到以下异常: There is no FormControl instance attached to form control element with name: controlName。 整个东西由 3 个元素组成:2 个组件 SelectProfileComponentSearchProfileComponent 和一个服务 ProfileServiceProfileService 包含 ActiveProfile,这是 selected 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);
        });
    }
}

SelectProfileComponent 包含一个配置文件列表,当 SearchProfile 被 select 编辑时会触发 change 事件。

<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);
    }
}

SearchProfileComponent 有一个 SubscriptionactiveProfile,应该显示 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]
        });
    }
}

您的 FormGroup 创建有点错误。您不需要将控件名称括在方括号和双引号中。在官方 angular 文档中阅读有关 reactive forms 的更多信息。

将您创建的 FormGroup 更改为:

this.profileForm = this.formBuilder.group({
            ["name"]: [this.searchProfile.name, null]
});

对此:

this.profileForm = this.formBuilder.group({
            name: [this.searchProfile.name]
});

并且您应该对 html 进行一些更改。提示:如果您使用的是响应式表单,则不需要 [(ngModel)] 输入,请将其删除。这里是html为您输入:

<form [formGroup]="profileForm" *ngIf="!isLoading">
    <input type="text" name="name" formControlName="name" />
</form>

我是这样解决的:

之前:

formControlName="description"

之后:

[formControl]="form.controls['description']"

我通过在组件构造函数中初始化表单一次解决了这个问题 即

constructor() {
        this.form = this.initForm();
}

ngOnInit() {
        this.form = this.initForm();
}

并在没有构造函数的组件任意位置移除组件中表单的re-initialization 即

this.form = this.initForm();

我发现使用 [formControl]="$form.something" 方法效果很好。笨重,但这 Angular 适合你。