I'm trying to create an Angular Elements component to embed in a hosted web page, and I want my component to contain an Angular reactive form.
Build fails with error: "Can't bind to 'formGroup' since it isn't a known property of 'form'."
Can anyone identify what I'm doing wrong?
Edit: This is Angular 9 and TypeScript 3.7 running on the latest version of the Angular CLI. Stackblitz: https://stackblitz.com/edit/angular-57wq1s
Edit 2: The updated Stackblitz code no longer throws a compile-time error, but the control does not finish rendering. The Submit button does not render. If I remove the 'formControlName' directive from the password input, the component does render completely.
Edit 3: StackBlitz seems to be working now, but if I download to my computer, npm install and ng serve, I get the problem where it renders the textbox but not the button.
Index.html
<!doctype html>
<html lang="en">
<body>
<app-password-reset></app-password-reset>
</body>
</html>
app.module.ts
@NgModule({
declarations: [],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
entryComponents: [PasswordResetComponent],
providers: [],
})
export class AppModule {
constructor(injector: Injector) {
const passwordResetElement = createCustomElement(PasswordResetComponent, {injector});
customElements.define('app-password-reset', passwordResetElement);
}
ngDoBootstrap(): void { }
}
password-reset.component.ts
@Component({
template: `
<form [formGroup]="resetForm" (ngSubmit)="formSubmit($event)">
<input type="password" formControlName="password">
<button type="submit">Reset Password</button>
</form>`
})
export class PasswordResetComponent {
constructor(private fb: FormBuilder) { }
resetForm = this.fb.group({
password: [null, Validators.required],
});
formSubmit(event: Event) {
alert('it works');
}
}