I would like to know if it is possible render a reactive form just when a function is called(instead of in ngOnInit()) using TemplateRef and ViewContainerRef. Here is my code:
component.html
<div
class="group"
(mouseover)="showActions = true"
(mouseleave)="showActions = false"
>
<dt>Username:</dt>
<dd> - </dd>
<div class="actions">
<a
mat-button (click)="editUsername()"
>Edit</a
>
</div>
<ng-container #editUsernameContainer></ng-container>
</div>
.
.
.
<ng-template #tmplEditUsername>
<div class="username-form">
<form [formGroup]="changeUsernameForm">
<input matInput #input placeholder="Username" formControlName="username"/>
<div class="form-actions">
<a mat-button>Cancel</a>
<button mat-raised-button (click)="updateUsername()">
Update
</button>
</div>
</form>
</div>
</ng-template>
component.ts
export class MyComponent implements OnInit {
@ViewChild('editUsernameContainer', { read: ViewContainerRef, static: true }) editUsernameContainer: ViewContainerRef;
@ViewChild('tmplEditUsername', { read: TemplateRef, static: true }) tmplEditUsername: TemplateRef<any>;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {}
editUsername() {
this.editUsernameContainer.createEmbeddedView(this.tmplEditUsername);
this.myForm = this.formBuilder.group({
username: new FormControl(''),
});
}
}
I am having this error in console:
Cannot read properties of undefined (reading 'createFormGroup') TypeError: Cannot read properties of undefined (reading 'createFormGroup')
Any solution?