Readonly/Disabled 输入未在 Angular 中提交
Readonly/Disabled input not submitting in Angular
我在 Angular 中创建表单时遇到问题 2. 我需要提交一些由服务动态生成的值。
使用 Angular 的 http 模块,我得到一个价格,然后用户现在可以准确地知道多少比特币是 X 数量的智利比索。我要做的是提交所有这些数据。我做不到,因为 angular 表单没有提交 disabled/readonly 输入。这些输入是汇率和目标金额(相当于比特币)。
我尝试了模板驱动方法和数据驱动方法,但都没有成功。我什至无法在控制台中记录这些值。如果我从输入中删除 disabled 或 readonly 属性,这些值将记录在控制台中并提交到我的数据库。
感谢您花时间帮助我。这是我的代码:
component.ts
import { Component, OnInit } from '@angular/core';
import { SurbtcService } from '../services/exchange/surbtc.service';
import { FormBuilder, FormGroup, Validators, FormControl, NgForm } from "@angular/forms";
import { FiredbService } from '../services/firedb.service';
import * as firebase from 'firebase/app';
@Component({
selector: 'my-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
//myForm2: FormGroup;
//error = false;
//errorMessage = '';
constructor(private surbtcService: SurbtcService, private fb: FormBuilder, private dbfr: FiredbService) {
/*
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [''],
rate: [''],
descripcion: ['']
});
*/ }
//Form
info = {
rate: '',
btcmount: ''
};
onSub() {
console.log(this.info);
}
//surbtcservice
prices: any;
baseAmount: 1;
get exchangeRate() {
return this.prices * 0.989
}
get targetAmount() {
return this.baseAmount / this.exchangeRate;
}
ngOnInit() {
this.surbtcService.getPrices()
.subscribe(prices => {
this.prices = prices.ticker.min_ask[0];
console.log(prices.ticker.min_ask[0]);
});
} }
如您在我的代码中所见,除了用于提交表单的函数外,数据驱动方法已被注释。
html:
<form (ngSubmit)="onSub(f)" #f="ngForm">
<div class="form-group">
<md-input-container class="full-width">
<input mdInput type="number" id="montoclp" name="clpmount" placeholder="Ingrese el monto en CLP" [(ngModel)]="baseAmount" name="clp">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="btcmount" [ngModel]="info.btcmount" placeholder="Monto en BTC" id="vat" [value]="targetAmount | number:'1.8-8'" readonly>
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="rate" [ngModel]="info.rate" placeholder="Tasa de cambio" id="street" [value]="exchangeRate | number:'1.0-0'" readonly>
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="mail" class="form-control" ngModel name="email" placeholder="E-mail cliente (Opcional)">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="text" class="form-control" ngModel name="descripcion" placeholder="Descripción pago (Opciona)">
</md-input-container>
</div>
<button md-raised-button type="submit" color="primary" class="btn-w-md">Confirmar</button><div class="divider divider-sm"></div>
</form>
再次感谢!
编辑!!!:
数据驱动html:
<form [formGroup]="myForm2" (ngSubmit)="onSub()">
<div class="form-group">
<md-input-container class="full-width">
<input mdInput type="number" id="montoclp" name="clpmount" placeholder="Ingrese el monto en CLP" formControlName="clpmount" [(ngModel)]="baseAmount">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="btcmount" placeholder="Monto en BTC" formControlName="btcmount" [value]="targetAmount | number:'1.8-8'">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="rate" formControlName="rate" placeholder="Tasa de cambio" [value]="exchangeRate | number:'1.0-0'">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="mail" class="form-control" formControlName="email" name="email" placeholder="E-mail cliente (Opcional)">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="text" class="form-control" formControlName="descripcion" name="descripcion" placeholder="Descripción pago (Opciona)">
</md-input-container>
</div>
<button md-raised-button type="submit" color="primary" class="btn-w-md">Confirmar</button><div class="divider divider-sm"></div>
</form>
如果您想要所有表单值,那么
更改此功能
onSub() {
console.log(this.info);
}
与
import { NgForm } from '@angular/forms';
onSub(formData : NgForm) {
console.log(formData.value);
}
Readonly/Disabled input not submitting in Angular
Disabled will never be submitted in any form
But Readonly , you will always get that in submitted form ,
Issue is with your code not with the angular
要以反应形式扩展 Vivek 的答案,您可以这样做:
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [''],
rate: [''],
descripcion: ['']
});
和
public onSub() {
console.log(this.myForm2.value);
}
或(在更改时):
ngOnInit() {
...
this.form.valueChanges.subscribe(value => console.log(value));
}
基本上你想从实际表单中获取数据,而不是从你的模型中获取数据。同样的事情也适用于模型驱动,正如 Vivek 概述的那样(传递表单而不是使用模型)
您可以使用 getRawValue()
,它会为您提供所有表单值,甚至是禁用的表单值。
所以当您的表单看起来像这样时:
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [{value: '', disabled: true}],
....
});
使用
this.myForm2.getRawValue()
将在上面的示例中包含 btcmount
值。
编辑:
查看模板...在反应形式中,Angular 忽略 value
和 [value]
。我们可以克服这一点并使用单向绑定,我也建议您对字段 clpAmount
和设置 [ngModel]="baseAmount"
执行此操作。为什么?好吧,首先 ngModel
指令甚至没有包含在 ReactiveFormsModule
中,因此,即使没有在文档中明确提及,我们也可以假设它们不应该一起使用。我们也可以这样理解,如果一起使用[(ngModel)]
,我们会有两个双向绑定,这有时会导致问题。
[ngModel]
更好,因为 Angular 只是将 TS 的值绑定到模板,并不关心接下来会发生什么。在某些情况下,[ngModel]
可用于反应形式,这就是其中一种情况。即使响应式形式忽略 value
,它也不会忽略 ngModel
,所以我们可以在这里很好地使用它!所以交换 [value]
与 [ngModel]
:
<input mdInput formControlName="btcmount" [ngModel]="targetAmount | number:'1.8-8'">
<input mdInput formControlName="rate" placeholder="Tasa de cambio" [ngModel]="exchangeRate | number:'1.0-0'">
现在,当您使用 getRawValue()
时,您将拥有所有值 :)
我在 Angular 中创建表单时遇到问题 2. 我需要提交一些由服务动态生成的值。 使用 Angular 的 http 模块,我得到一个价格,然后用户现在可以准确地知道多少比特币是 X 数量的智利比索。我要做的是提交所有这些数据。我做不到,因为 angular 表单没有提交 disabled/readonly 输入。这些输入是汇率和目标金额(相当于比特币)。
我尝试了模板驱动方法和数据驱动方法,但都没有成功。我什至无法在控制台中记录这些值。如果我从输入中删除 disabled 或 readonly 属性,这些值将记录在控制台中并提交到我的数据库。
感谢您花时间帮助我。这是我的代码:
component.ts
import { Component, OnInit } from '@angular/core';
import { SurbtcService } from '../services/exchange/surbtc.service';
import { FormBuilder, FormGroup, Validators, FormControl, NgForm } from "@angular/forms";
import { FiredbService } from '../services/firedb.service';
import * as firebase from 'firebase/app';
@Component({
selector: 'my-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
//myForm2: FormGroup;
//error = false;
//errorMessage = '';
constructor(private surbtcService: SurbtcService, private fb: FormBuilder, private dbfr: FiredbService) {
/*
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [''],
rate: [''],
descripcion: ['']
});
*/ }
//Form
info = {
rate: '',
btcmount: ''
};
onSub() {
console.log(this.info);
}
//surbtcservice
prices: any;
baseAmount: 1;
get exchangeRate() {
return this.prices * 0.989
}
get targetAmount() {
return this.baseAmount / this.exchangeRate;
}
ngOnInit() {
this.surbtcService.getPrices()
.subscribe(prices => {
this.prices = prices.ticker.min_ask[0];
console.log(prices.ticker.min_ask[0]);
});
} }
如您在我的代码中所见,除了用于提交表单的函数外,数据驱动方法已被注释。
html:
<form (ngSubmit)="onSub(f)" #f="ngForm">
<div class="form-group">
<md-input-container class="full-width">
<input mdInput type="number" id="montoclp" name="clpmount" placeholder="Ingrese el monto en CLP" [(ngModel)]="baseAmount" name="clp">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="btcmount" [ngModel]="info.btcmount" placeholder="Monto en BTC" id="vat" [value]="targetAmount | number:'1.8-8'" readonly>
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="rate" [ngModel]="info.rate" placeholder="Tasa de cambio" id="street" [value]="exchangeRate | number:'1.0-0'" readonly>
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="mail" class="form-control" ngModel name="email" placeholder="E-mail cliente (Opcional)">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="text" class="form-control" ngModel name="descripcion" placeholder="Descripción pago (Opciona)">
</md-input-container>
</div>
<button md-raised-button type="submit" color="primary" class="btn-w-md">Confirmar</button><div class="divider divider-sm"></div>
</form>
再次感谢!
编辑!!!:
数据驱动html:
<form [formGroup]="myForm2" (ngSubmit)="onSub()">
<div class="form-group">
<md-input-container class="full-width">
<input mdInput type="number" id="montoclp" name="clpmount" placeholder="Ingrese el monto en CLP" formControlName="clpmount" [(ngModel)]="baseAmount">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="btcmount" placeholder="Monto en BTC" formControlName="btcmount" [value]="targetAmount | number:'1.8-8'">
</md-input-container>
<md-input-container class="full-width">
<input mdInput class="form-control" name="rate" formControlName="rate" placeholder="Tasa de cambio" [value]="exchangeRate | number:'1.0-0'">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="mail" class="form-control" formControlName="email" name="email" placeholder="E-mail cliente (Opcional)">
</md-input-container>
<md-input-container class="full-width">
<input mdInput type="text" class="form-control" formControlName="descripcion" name="descripcion" placeholder="Descripción pago (Opciona)">
</md-input-container>
</div>
<button md-raised-button type="submit" color="primary" class="btn-w-md">Confirmar</button><div class="divider divider-sm"></div>
</form>
如果您想要所有表单值,那么
更改此功能
onSub() {
console.log(this.info);
}
与
import { NgForm } from '@angular/forms';
onSub(formData : NgForm) {
console.log(formData.value);
}
Readonly/Disabled input not submitting in Angular
Disabled will never be submitted in any form But Readonly , you will always get that in submitted form ,
Issue is with your code not with the angular
要以反应形式扩展 Vivek 的答案,您可以这样做:
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [''],
rate: [''],
descripcion: ['']
});
和
public onSub() {
console.log(this.myForm2.value);
}
或(在更改时):
ngOnInit() {
...
this.form.valueChanges.subscribe(value => console.log(value));
}
基本上你想从实际表单中获取数据,而不是从你的模型中获取数据。同样的事情也适用于模型驱动,正如 Vivek 概述的那样(传递表单而不是使用模型)
您可以使用 getRawValue()
,它会为您提供所有表单值,甚至是禁用的表单值。
所以当您的表单看起来像这样时:
this.myForm2 = this.fb.group({
email: ['', Validators.email],
clpmount: ['', Validators.required],
btcmount: [{value: '', disabled: true}],
....
});
使用
this.myForm2.getRawValue()
将在上面的示例中包含 btcmount
值。
编辑:
查看模板...在反应形式中,Angular 忽略 value
和 [value]
。我们可以克服这一点并使用单向绑定,我也建议您对字段 clpAmount
和设置 [ngModel]="baseAmount"
执行此操作。为什么?好吧,首先 ngModel
指令甚至没有包含在 ReactiveFormsModule
中,因此,即使没有在文档中明确提及,我们也可以假设它们不应该一起使用。我们也可以这样理解,如果一起使用[(ngModel)]
,我们会有两个双向绑定,这有时会导致问题。
[ngModel]
更好,因为 Angular 只是将 TS 的值绑定到模板,并不关心接下来会发生什么。在某些情况下,[ngModel]
可用于反应形式,这就是其中一种情况。即使响应式形式忽略 value
,它也不会忽略 ngModel
,所以我们可以在这里很好地使用它!所以交换 [value]
与 [ngModel]
:
<input mdInput formControlName="btcmount" [ngModel]="targetAmount | number:'1.8-8'">
<input mdInput formControlName="rate" placeholder="Tasa de cambio" [ngModel]="exchangeRate | number:'1.0-0'">
现在,当您使用 getRawValue()
时,您将拥有所有值 :)