Angular 无法分配给';总计';因为它是常量或只读属性
我在项目中运行Angular 无法分配给';总计';因为它是常量或只读属性,angular,typescript,angular5,readonly,Angular,Typescript,Angular5,Readonly,我在项目中运行ngbuild--prod 我发现的错误是: src\app\components\xxxx\xxxx.component.html(116100)::无法分配 设置为“total”,因为它是常量或只读属性 在这一行中,我有以下代码: 我使用了[(ngModel)]=“total”-->因为我希望即使不修改值也能保存它。如果我使用了[value]=“total”,则此错误不存在,但如果我不修改,则不会保存我的值 这个总数是通过这个函数得到的,它是只读的 get total()
ngbuild--prod
我发现的错误是:
src\app\components\xxxx\xxxx.component.html(116100)::无法分配
设置为“total”,因为它是常量或只读属性
在这一行中,我有以下代码:
我使用了[(ngModel)]=“total”
-->因为我希望即使不修改值也能保存它。如果我使用了[value]=“total”
,则此错误不存在,但如果我不修改,则不会保存我的值
这个总数是通过这个函数得到的,它是只读的
get total() {
return this.products
.map(p => p.p_Unit_price * p.p_Quantity)
.reduce((a, b) => a + b, 0);
}
这个总数,显示所有产品的总数
如何修改这个代码,那工作正常吗
编辑:
Html代码:
产品类型标识
产品标识
单价
p_数量
{{item.p_Product_type_id}
{{item.p_product_id}
{{item.p_单价}
{{item.p_Quantity}
总计{{Total}}ALL
小计
登记
Ts代码:
export class component implements OnInit {
constructor(.............
) {
this.addsale = this.fb.group({
'Subtotal': new FormControl('', Validators.required),
'products': this.fb.array([]),
'total': new FormControl('', Validators.required),
});
}
ngOnInit() {
this.allproducts();
allproducts() {
this.products = this.ps.getProduct();
}
onaddsale() {
this.areWeWaiting = true;
let sale = this.addsale.value
sale.products = this.products
let newSale = new Sale(sale);
this.ws.saleitemcreate(newSale).subscribe(
result => {
if (result === true) {
Materialize.toast('Successfully', 4000);
} else {
this.areWeWaiting = false;
}
},
error => {
this.areWeWaiting = false;
}
);
}
get total() {
return this.products
.map(p => p.p_Unit_price * p.p_Quantity)
.reduce((a, b) => a + b, 0);
}
}
使用[value]=“total”
并在保存时将总数添加到保存的值中。您不需要使用表单来收集要保存的值。您可以在组件中处理此问题
我要说的是,使用ngModel
仅仅传递一些值来节省是一种有点老套的方式
我会编辑onaddsale
方法,将总额添加到销售中。然后您可以从fb.group()
中删除total
,也可以从模板中删除formControlName=“total”
,因为您并不真正需要它
onaddsale() {
this.areWeWaiting = true;
let sale = this.addsale.value;
sale.products = this.products;
// pass total here
sale.total = this.total;
let newSale = new Sale(sale);
...
}
你可能需要重复的请再看看,我编辑我的文章与完整的代码。在这个演示中,我有时需要更改值,演示示例是:您能帮我更改支付金额中的值吗?Thanxy您可以在函数
UpdateForm(event)
中使用setValue
中描述的setValue
,但没有任何更改。当您更改值并提交表单时,请查看控制台。我可以在控制台中看到值的更改。我不明白你想达到什么目的。也许你需要提出另一个问题。