简体   繁体   English

Angular8 表单输入字段被禁用

[英]Angular8 form input fields disabled

I am using Angular8 and have a form used to update a product.我正在使用 Angular8 并且有一个用于更新产品的表单。 My problem however is this forms input fields and submit button is disabled.然而,我的问题是这个表单输入字段和提交按钮被禁用。 Can anyone advise what I am doing wrong?谁能建议我做错了什么? I would expect to be able to edit the input fields text and submit the form.我希望能够编辑输入字段文本并提交表单。

html: html:

<div class="bodyClass">
    <h1>{{title | titlecase}}</h1>
    <div class="card">
        <div class="card-body">
            <form *ngIf="angForm && productData" [formGroup]="angForm" novalidate>
                <div class="form-group">
                    <label class="col-md-4">Product Name</label>
                    <input type="text" class="form-control" formControlName="name" #name/>
                </div>
                <div *ngIf="angForm.controls['name'].invalid && (angForm.controls['name'].dirty || angForm.controls['name'].touched)"
                    class="alert alert-danger">
                    <div *ngIf="angForm.controls['name'].errors.required">
                        Product Name is required.
                    </div>
                </div>
                <div class="form-group">
                    <button (click)="updateProduct(name.value)" type="submit" class="btn btn-primary"
                        [disabled]="angForm.invalid">
                        Update Product
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

component:成分:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { PermissionsService } from '../../services/permissions.service';
import { Permissions } from '../../model/permissions';
import { ProductsService } from '../../services/products.service';
import { DataService } from '../../services/data.service';
import { Product } from '../../model/product';

@Component({
  selector: 'app-productsupdate',
  templateUrl: './productsupdate.component.html',
  styleUrls: ['./productsupdate.component.css']
})
export class ProductsupdateComponent implements OnInit {

  angForm: FormGroup;
  private permissionsObservable: Observable<Permissions>; 
  private showData: boolean = true;
  private title: string;
  private productData: Product;

  constructor(private _permissionsService: PermissionsService, private _productService: ProductsService,
              private dataService: DataService, private fb: FormBuilder) {
                this.createForm();
  }

  ngOnInit() {
    this.title = 'Update Product';
    if (this.dataService.serviceData) {
      console.log(this.dataService.serviceData);
      this.productData = this.dataService.serviceData; 
    }
  }

  ngDoCheck() {
    if (this.productData) {
      this.angForm.get('name').setValue(this.productData.name);
    }
  }

  createForm() {
    this.angForm = this.fb.group({
      name: ['', Validators.required ],
      decription: ['', Validators.required ],
      status: ['', Validators.required ]
    });
  }

  updateProduct() {
    console.log('submit');
  }
}

Screenprint:丝印:

可以看到按钮被禁用,输入文本不可编辑

You can see that the button is disabled and the input text is non-editable.您可以看到该按钮已禁用且输入文本不可编辑。

You are using ngDoCheck , so everytime angular runs it, for example when you are trying to type, and productData is populated, 您正在使用ngDoCheck ,因此每次使用angular时都会运行它,例如当您尝试键入时,并且填充了productData

this.angForm.get('name').setValue(this.productData.name);

is run, and thus always setting the value making it seem that you cannot edit the field. 运行,因此始终设置值使您看起来无法编辑该字段。

Since this can also be a race condition, as forms are asynchronous, I suggest building the form after getting value to productData (if getting value). 由于这也可能是竞争条件,因为表单是异步的,我建议在获取productData值之后构建表单(如果获取值)。 So remove the createForm() function from constructor and add it later on: 因此,从构造函数中删除createForm()函数,稍后再添加:

if (this.dataService.serviceData) {
  console.log(this.dataService.serviceData);
  this.productData = this.dataService.serviceData; 
}
this.createForm();

Modify the createForm function a bit: createForm修改createForm函数:

createForm() {
  this.angForm = this.fb.group({
    name: [this.productData.name || '', Validators.required ],
    description: [this.productData.description || ''],
    status: [this.productData.status || '']
  });
}

您的表单还包含所需的描述和状态,并且您不提供这些描述和状态,因此您的表单无效且您无法提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM