0
Look at the structure of the project
The page estilo-cadastro.component.html
is found that way as you can see below.
<div class="container">
<form #f="ngForm" autocomplete="off" (ngSubmit)="salvar(f)">
<div class="ui-g">
<div class="ui-g-12">
<h1>Novo Estilo</h1>
</div>
<app-layout-cadastro-estilo [estilo]="estilo"></app-layout-cadastro-estilo>
</div>
</form>
</div>
I’m trying to create a component with the code snippet below and I’m not getting.
<app-layout-cadastro-estilo [estilo]="estilo"></app-layout-cadastro-estilo>
<div class="ui-g-12 ui-md-9 ui-fluid">
<label>Nome do Estilo </label>
<input pInputText type="text" name="nome" [(ngModel)]="estilo.nome" ngModel #nome="ngModel" required minlength="5">
<app-message [control]="nome" error="required" text="Informe o Estilo"></app-message>
<app-message [control]="nome" error="minlength" text="Mínimo de {{ nome.errors?.minlength?.requiredLength }} caracteres"></app-message>
</div>
<div class="ui-g-12">
<p-footer>
<button pButton type="submit" label="Salvar" [disabled]="f.invalid"></button>
<button pButton type="button" label="Novo" class="ui-button-info"></button>
<a href="javascript:;">Voltar para a pesquisa</a>
</p-footer>
</div>
This is the file layout-cadastro-estilo.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-layout-cadastro-estilo',
templateUrl: './layout-cadastro-estilo.component.html',
styleUrls: ['./layout-cadastro-estilo.component.css']
})
export class LayoutCadastroEstiloComponent {
@Input() estilo = [];
}
And this is the file estilo-cadastro.component.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ToastyService } from 'ng2-toasty';
import { Estilo } from './../../core/model';
import { ErroHandlerService } from './../../core/erro-handler.service';
import { EstiloService } from './../estilo.service';
@Component({
selector: 'app-estilo-cadastro',
templateUrl: './estilo-cadastro.component.html',
styleUrls: ['./estilo-cadastro.component.css']
})
export class EstiloCadastroComponent implements OnInit {
private estilo = new Estilo();
constructor(
private estiloService: EstiloService,
private erroHandler: ErroHandlerService,
private toasty: ToastyService
) { }
ngOnInit() {}
salvar(form: FormControl) {
this.estiloService.adicionar(this.estilo).then(() => {
this.toasty.success('Cerveja adicionado com sucesso!');
form.reset();
this.estilo = new Estilo();
}).catch(erro => this.erroHandler.handle(erro));
}
}
That’s the error message you’re giving:
Please, how do I resolve this? The error message is giving in this line of code:
<input pInputText type="text" name="nome" [(ngModel)]="estilo.nome"
when I include this line @Input() form: Formgroup; it causes an error in the visual code console.
– wladyband
Make sure everything is imported correctly, I declared the type
FormGroup
. You can remove and leave it as: @Input() form; or importFormGroup
.– celsomtrindade