[英]Setting values of input fields with Angular 6
我在使用Angular设置输入元素的值时遇到了一些麻烦。
我试图通过此方法在我的应用程序中设置动态创建的输入元素的值:
copyPricingToAll(): void {
var copyValue: string = document.getElementById("priceInputGlobal").value;
this.currentItem.orderLines.forEach(orderLine => {
document.getElementById("priceInput-" + orderLine.id).setAttribute("value", copyValue);
});
}
我正在创建这样的行:
<ng-container *ngFor="let orderLine of currentItem.orderLines let i=index">
<tr>
<td>{{getCorrectTimeString(orderLine)}}</td>
<td>{{orderLine.quantity}}</td>
<td><input id="priceInput-{{orderLine.id}}" type="number" value="{{orderLine.price}}"></td>
</tr>
</ng-container>
不幸的是.value不被认为是有效的操作。 我不确定如何在angular中正确设置动态创建元素的值。 我希望有人能够帮助我解决这个问题。
您应该使用以下内容:
<td><input id="priceInput-{{orderLine.id}}" type="number" [(ngModel)]="orderLine.price"></td>
您需要在inputs
部分app.module
添加到FormsModule
,如下所示:
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
...
],
imports: [
BrowserModule,
FormsModule
],
..
ngModel
周围括号的使用如下:
[]
显示它正在从您的TS文件中获取输入。 此输入应为公共成员变量。 从TS到HTML的单向绑定。
()
表明它将HTML文件的输出转换为TS文件中的变量。 从HTML到TS的单向绑定。
[()]
都是(例如双向绑定)
有关更多信息,请参见此处: https : //angular.io/guide/template-syntax
我还建议用这样的东西替换id="priceInput-{{orderLine.id}}"
[id]="getElementId(orderLine)"
其中getElementId(orderLine)
返回TS文件中的元素Id,可以任意使用你需要引用元素(以避免简单的错误,比如在一个地方调用priceInput1
在另一个地方调用priceInput-1
。(如果你还需要在其他地方通过它的Id访问输入)
作为替代,您可以使用反应形式 。 这是一个例子: https : //stackblitz.com/edit/angular-pqb2xx
模板
<form [formGroup]="mainForm" ng-submit="submitForm()">
Global Price: <input type="number" formControlName="globalPrice">
<button type="button" [disabled]="mainForm.get('globalPrice').value === null" (click)="applyPriceToAll()">Apply to all</button>
<table border formArrayName="orderLines">
<ng-container *ngFor="let orderLine of orderLines let i=index" [formGroupName]="i">
<tr>
<td>{{orderLine.time | date}}</td>
<td>{{orderLine.quantity}}</td>
<td><input formControlName="price" type="number"></td>
</tr>
</ng-container>
</table>
</form>
零件
import { Component } from '@angular/core';
import { FormGroup, FormControl, FormArray } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 6';
mainForm: FormGroup;
orderLines = [
{price: 10, time: new Date(), quantity: 2},
{price: 20, time: new Date(), quantity: 3},
{price: 30, time: new Date(), quantity: 3},
{price: 40, time: new Date(), quantity: 5}
]
constructor() {
this.mainForm = this.getForm();
}
getForm(): FormGroup {
return new FormGroup({
globalPrice: new FormControl(),
orderLines: new FormArray(this.orderLines.map(this.getFormGroupForLine))
})
}
getFormGroupForLine(orderLine: any): FormGroup {
return new FormGroup({
price: new FormControl(orderLine.price)
})
}
applyPriceToAll() {
const formLines = this.mainForm.get('orderLines') as FormArray;
const globalPrice = this.mainForm.get('globalPrice').value;
formLines.controls.forEach(control => control.get('price').setValue(globalPrice));
// optionally recheck value and validity without emit event.
}
submitForm() {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.