I'm following a online course but I have lots of problems with that.
I depends on I put on my code I get
type 'abstractcontrol' is missing the following properties from type 'formcontrol': registerOnChange, registerOnDisable, _applyFormState
or
type Abstractcontrol is not assignable to type formcontrol
In the TS of my Component I have
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { DestinoViaje } from '../models/destino-viaje.model';
@Component({
selector: 'app-form-destino-viaje',
templateUrl: './form-destino-viaje.component.html',
styleUrls: ['./form-destino-viaje.component.css']
})
export class FormDestinoViajeComponent implements OnInit {
@Output() onItemAdded: EventEmitter<DestinoViaje>;
fg: FormGroup;
constructor(fb: FormBuilder) {
this.onItemAdded = new EventEmitter();
this.fg = fb.group({
nombre: [''],
url: ['']
});
console.log(this.fg);
}
ngOnInit(): void {
}
guardar(nombre: string, url: string): boolean {
let d = new DestinoViaje(nombre, url);
this.onItemAdded.emit(d);
return false;
}
}
and in the HTML of my Component I Have
<form
[formGroup]="fg"
(ngSubmit)="guardar(
fg.controls['nombre'].value,
fg.controls['url'].value
)">
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" class="form-control"
id="nombre" placeholder="Ingresar nombre..."
[formControl]="fg.controls['nombre']">
</div>
<div class="form-group">
<label for="Imagen Url">Imagen Url</label>
<input type="text" class="form-control"
id="imagenUrl" placeholder="Ingresar url..."
[formControl]="fg.controls['url']">
</div>
<button type="submit" class="btn btn-primary">Guardar!</button>
</form>
I'm not sure in which version the example is, but I'm using Angular 11.05
Thanks in advance.
Regards
Nicolas
This is because your
nombreandurlare not control fields, you are making them an array of an empty string. you should create them likeSo you create a
FormControlinstead ofArray<string>Here's a template sample using reactive forms:
Some notes:
Given your confusion, if you check here you will see that there's an example doing what you did:
Just using a
FormControlin case you have a single input that doesn't belong to any<form>orFormGroup. When you have a full form, if you scroll down, you'll see an example that looks more what I gave you in my last edit of my answer.Final answer
No it's not because different versions of Angular but different use-cases of the
ReactiveFormsof Angular. One uses a form and the other does not.