【发布时间】:2018-08-03 21:59:01
【问题描述】:
我正在尝试在表单中创建一些数字输入,旁边有 + 和 - 按钮,可以增加/减少数值。我正在使用存储在几个数组中的值创建带有 *ngFor 循环的输入和按钮。
但是我想不出一种方法来将输入绑定到同样在同一个 *ngFor 循环中创建的按钮。我一直遇到这个错误——Cannot assign to a reference or variable!
我尝试使用 ngModel 并仅与 [value] 绑定。无论哪种方式,我都会遇到相同的错误。
还有其他方法吗?
Stackblitz 演示 -- https://stackblitz.com/edit/angular-m3gkrq
我尝试过的代码 --
<div class="border rounded p-4 m-1" *ngFor="let type of types">
<div *ngFor="let subType of subTypes[type]">
<label for="{{subType}}SlotsInput">{{type | titlecase}} : {{subType}}</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary" type="button">-</button>
<button class="btn btn-outline-secondary" type="button" (click)="(type+subType)++">+</button>
</div>
<input type="number" class="form-control" placeholder="0" id="{{subType}}SlotsInput" min="0" max="10" [name]="type+subType" [(ngModel)]="type+subType">
</div>
</div>
</div>
--
export class AppComponent {
types = ["dog", "horse", "hen", "elephant"];
subTypes = {
dog:["labrador", "vizsla"],
horse:["arabian","shire","belgian"],
hen:["plymouth rock", "leghorn", "bramha"],
elephant:["african", "asian"]
};
编辑:如果不清楚,我希望 + 和 - 按钮在它们旁边的输入字段中增加/减少数字。
【问题讨论】:
-
我认为您必须使用表单生成器来执行此操作。我怎样才能重现你的错误?
-
尝试在stackblitz demo中绑定ngModel看看错误
标签: angular