我在 Angular 2 中创建表单时遇到问题。我需要提交一些由服务动态生成的值。使用 Angular 的 http 模块,我得到一个价格,然后用户现在可以确切地知道多少比特币是 X 数量的智利比索。我要做的是提交所有这些数据。我不能这样做,因为角度表单没有提交禁用/只读输入。这些输入是汇率和目标金额(相当于比特币)。
我尝试了模板驱动方法和数据驱动方法,但没有成功。我什至无法在控制台中记录值。如果我从输入中删除 disabled 或 readonly 属性,则值将记录在控制台中并提交到我的数据库。
感谢您花时间帮助我。这是我的代码:
组件.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>