3

我正在学习在线课程,但我有很多问题。

我取决于我输入我得到的代码

“abstractcontrol”类型缺少“formcontrol”类型的以下属性:registerOnChange、registerOnDisable、_applyFormState

或者

类型 Abstractcontrol 不可分配给类型 formcontrol

在我的组件的 TS 中,我有

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;

  }

}

在我拥有的组件的 HTML 中

<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>

我不确定示例是哪个版本,但我使用的是 Angular 11.05

提前致谢。

问候

尼古拉斯

4

2 回答 2

1

这是因为您的nombreurl不是控制字段,您正在使它们成为一个空字符串的数组。你应该像这样创建它们

this.fg = fb.group({
      nombre: this.fb.control(/*initial value*/'', /*validators that they should pass*/[]')',
      url:this.fb.control(/*initial value*/'', /*validators that they should pass*/[]')'
});

所以你创建一个FormControl而不是Array<string>

这是使用反应形式的模板示例:

<!-- component.template.html -->

<form [formGroup]="group">
    <input type="email" formControlName="email">
    <input type="password" formControlName="passw">
    <!-- more inputs maybe -->
</form>
@Component({...})
export class YourComponent implements OnInit {

    // This is our reactive form that we want to use
    group: FormGroup;

    constructor(private fb: FormBuilder) {}

    ngOnInit() {
        const fb = this.fb; // So we write less code
        this.group = fb.group({
            // On the left side we use the formControlName that we are going to use in the html
            // then, on the control we pass the default value of the input and an array of validators. 
            // Validators are just functions that may return errors given a certain control input.
            email: fb.control('', [Validators.required]),
            // Remember that passw is what we used on the formControlName of the template
            passw: fb.control('', [Validators.required])
        });
    }

}

一些注意事项:

考虑到您的困惑,如果您在此处查看,您会看到有一个示例正在执行您所做的操作:

只需使用 aFormControl以防您有一个不属于任何<form>or的输入FormGroup。当你有一个完整的表格时,如果你向下滚动,你会看到一个看起来更像我在我上次编辑我的答案时给你的例子。

最终答案

不,这不是因为 Angular 的不同版本,而是 Angular 的不同用例ReactiveForms。一个使用表格,另一个不使用。

于 2020-12-26T22:23:06.733 回答
0

我也有同样的问题,然后我改了:fg: FormGroup; fg 上:任何;

于 2021-06-08T13:06:28.050 回答