Tenho este código:
export class GruposPesquisarComponent extends GHComponente implements OnInit {
displayedColumns: string[] = ['select', 'nome', 'role', 'statusDoRegistro'];
public grupoPesquisarFormGroup: FormGroup;
public statusRegistros: EnumModel;
constructor(
private formBuilder: FormBuilder,
protected router: Router,
protected routaAtual: ActivatedRoute,
protected snackBar: MatSnackBar,
protected dialog: MatDialog,
protected service: GruposService,
protected alertService: AlertService,
private enumService: EnumService,
) {
super(
new GruposPaginacaoModel(),
'Grupos',
'grupos',
service,
false,
router,
routaAtual,
snackBar,
dialog,
alertService
);
}
public ngOnInit() {
super.ngOnInit();
this.criarFormGroup();
this.carregarStatusDoRegistro();
}
public criarFormGroup() {
this.grupoPesquisarFormGroup = this.formBuilder.group({
nome: new FormControl({value: ''}, Validators.required),
role: new FormControl({value: '', disabled: true}, Validators.required),
statusDoRegistro: new FormControl({value: '', disabled: true}, Validators.required)
});
}
public limpar() {
this.modelo = new GruposPaginacaoModel();
}
private carregarStatusDoRegistro() {
this.enumService.statusRegistros().subscribe(
(data: any) => {
this.statusRegistros = data;
}
);
}
}
Dá este erro:
ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup
directive and pass it an existing FormGroup instance (you can create one in your class).
Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});
at Function.push../node_modules/@angular/forms/fesm5/forms.js.ReactiveErrors.controlParentException (forms.js:1437)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._checkParentType (forms.js:5877)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._setUpControl (forms.js:5881)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName.ngOnChanges (forms.js:5803)
at checkAndUpdateDirectiveInline (core.js:22095)
at checkAndUpdateNodeInline (core.js:23363)
at checkAndUpdateNode (core.js:23325)
at debugCheckAndUpdateNode (core.js:23959)
at debugCheckDirectivesFn (core.js:23919)
at Object.eval [as updateDirectives] (GruposPesquisarComponent.html:31)
View_GruposPesquisarComponent_0 @ GruposPesquisarComponent.html:31
proxyClass @ compiler.js:18239
push../node_modules/@angular/core/fesm5/core.js.DebugContext_.logError @ core.js:24139
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:15772
(anonymous) @ core.js:18126
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
push../node_modules/zone.js/dist/zone.js.Zone.run @ zone.js:150
push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular @ core.js:17258
push../node_modules/@angular/core/fesm5/core.js.ApplicationRef.tick @ core.js:18126
(anonymous) @ core.js:18010
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
onInvoke @ core.js:17299
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:390
push../node_modules/zone.js/dist/zone.js.Zone.run @ zone.js:150
push../node_modules/@angular/core/fesm5/core.js.NgZone.run @ core.js:17213
next @ core.js:18010
schedulerFn @ core.js:13515
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:196
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:134
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ Subject.js:47
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ core.js:13499
checkStable @ core.js:17268
onHasTask @ core.js:17312
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.hasTask @ zone.js:443
push../node_modules/zone.js/dist/zone.js.ZoneDelegate._updateTaskCount @ zone.js:463
push../node_modules/zone.js/dist/zone.js.Zone._updateTaskCount @ zone.js:291
push../node_modules/zone.js/dist/zone.js.Zone.runTask @ zone.js:212
drainMicroTaskQueue @ zone.js:601
Promise.then (async)
scheduleMicroTask @ zone.js:584
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:413
push../node_modules/zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:238
push../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:258
scheduleResolveOrReject @ zone.js:879
ZoneAwarePromise.then @ zone.js:1012
push../node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule @ core.js:17803
./src/main.ts @ main.ts:12
__webpack_require__ @ bootstrap:78
0 @ main.ts:13
__webpack_require__ @ bootstrap:78
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1
GruposPesquisarComponent.html:31 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 63, nodeDef: {…}, elDef: {…}, elView: {…}}
Tenho este outro código:
export class GruposAlterarComponent extends GHComponente implements OnInit {
public grupoAlterarFormGroup: FormGroup;
constructor(
private formBuilder: FormBuilder,
protected router: Router,
protected routaAtual: ActivatedRoute,
protected snackBar: MatSnackBar,
protected dialog: MatDialog,
protected service: GruposService,
protected alertService: AlertService
) {
super(
new GruposModel(),
'Grupos',
'grupos',
service,
false,
router,
routaAtual,
snackBar,
dialog,
alertService
);
}
ngOnInit() {
super.ngOnInit();
this.criarFormGroup();
}
criarFormGroup() {
this.grupoAlterarFormGroup = this.formBuilder.group({
nome: new FormControl({value: ''}, Validators.required),
role: new FormControl({value: '', disabled: true}, Validators.required)
});
}
public limpar() {
this.modelo = new GruposModel();
}
}
O segundo não dá erro.
Não consegui identificar a diferença e o motivo.