【发布时间】:2019-02-01 17:50:41
【问题描述】:
当我在项目中使用此命令 ng build --prod 时,显示此错误。
无法分配给“total”,因为它是函数 get total(){}
我的功能是:
get total() {
return this.products
.map(p => p.Unit_price * p.Quantity)
.reduce((a, b) => a + b, 0);
}
HTML:
<form [formGroup]="addsale" (ngSubmit)="onaddsale()" >
<div class="row">
<div class="input-field col s2" style="float: right;">
<label for="total">Total {{total}} ALL</label>
<input formControlName="total" id="total" type="text" class="validate" [value]="total" [(ngModel)]="total">
</div>
<div class="input-field col s2" style="float: right;">
<label for="amount_paid">Amount Paid:</label>
<input formControlName="amount_paid" id="amount_paid" [value]="total" type="text" class="validate" [(ngModel)]="total">
</div>
<div class="input-field col s2" style="float: right;">
<label for="total">Subtotal</label>
<input formControlName="Subtotal" id="Subtotal" type="text" class="validate" [value]="total" [(ngModel)]="total">
</div>
</div>
</form>
Ts 形式:
this.addsale = this.fb.group({
'amount_paid': new FormControl('', Validators.required),
'Subtotal': new FormControl('', Validators.required),
'total': new FormControl('', Validators.required)
});
有什么想法吗?
【问题讨论】:
-
这对我有用。您使用的是什么 Angular 和 cli 版本?你能运行“ng serve”吗?
-
你试过在 total() 之前不使用 'get' 吗?
-
如果你只是'返回5',它是否有效; (例如)?
-
@ForestG "@angular/cdk": "^5.1.1", "@angular/cli": "^1.6.2",当我运行 ng serve 时,效果很好
-
你能说明你在哪里使用它吗?或者你在哪里使用
total变量?
标签: angular typescript