我正在尝试使用Angular2和Angular Material。我使用了*ngFor
让Angular为我生成<input>
元素。然而,在生成的网页中,生成的元素没有name
属性。
这是order-form.component.html
中的一部分代码,它要求用户输入不同种类水果的数量:
<md-list-item>
<md-icon md-list-icon>shopping_cart</md-icon>
<md-input-container *ngFor="let fruit of fruits" class="fruit-input">
<input mdInput [(ngModel)]="order[fruit]" [placeholder]="capitalize(fruit)"
[id]="fruit" name="{{fruit}}" required value="0" #fruitInput
(input)="onInput(fruitInput)">
</md-input-container>
</md-list-item>
这是相应的order-form.component.ts
:
import { Component, OnInit } from "@angular/core";
import { Order } from "app/order";
import { PAYMENTS } from "app/payments";
import { OrderService } from "app/order.service";
@Component({
selector: 'app-order-form',
templateUrl: './order-form.component.html',
styleUrls: ['./order-form.component.css']
})
export class OrderFormComponent implements OnInit {
order = new Order();
payments = PAYMENTS;
fruits: string[] = [
'apples',
'oranges',
'bananas'
];
constructor(public service: OrderService) {
}
ngOnInit() {
}
get totalCost() {
return this.service.getTotalCost(this.order);
}
onFocus(element: HTMLElement) {
element.blur();
}
onSubmit() {
console.log('onSubmit');
}
onInput(element: HTMLInputElement) {
console.log(element);
if (!this.service.isValidIntString(element.value)) {
window.alert(`Please input a correct number for ${element.name}`);
element.value = '0';
}
}
capitalize(str: string): string {
return this.service.capitalize(str);
}
get debug() {
return JSON.stringify(this.order, null, 2);
}
}
在Chrome浏览器中,当我右键点击“apples” <input>
元素时,该元素的 name
属性为空,但是 ng-reflect-name
属性正确地设置为 apples
。如何解决这个问题?此处没有 name
属性,但 ng-reflect-name
为 apples