简体   繁体   English

无法分配给“total”,因为它是一个常量或只读属性

[英]Cannot assign to 'total' because it is a constant or a read-only property

I run ng build --prod in my project.我在我的项目中运行ng build --prod

That I found is this error:我发现的是这个错误:

src\\app\\components\\xxxx\\xxxx.component.html(116,100): : Cannot assign to 'total' because it is a constant or a read-only property. src\\app\\components\\xxxx\\xxxx.component.html(116,100): :无法分配给 'total',因为它是一个常量或只读属性。

In this line I have this code:在这一行中,我有以下代码:

<input formControlName="total" id="total" type="text" class="validate" [(ngModel)]="total">

I used [(ngModel)]="total" --> because I want the value to be saved even without modifying it.我使用[(ngModel)]="total" --> 因为我希望即使不修改它也能保存该值。 If I used [value]="total" this error doesn't exist, but my value doesn't saved If I not modify.如果我使用[value]="total"此错误不存在,但如果我不修改,我的值不会保存。

and this total I get by this function in ts.我在 ts 中通过这个函数得到了这个总数。 This is Read Only这是只读的

get total() {
    return this.products
      .map(p => p.p_Unit_price * p.p_Quantity)
      .reduce((a, b) => a + b, 0);
}

This total, show me total from all product.这个总数,显示所有产品的总数。

How to modify this code, that working ok?如何修改此代码,工作正常吗?

Edit:编辑:

Html code: html代码:

<form [formGroup]="addsale" (ngSubmit)="onaddsale()">
  <table align="center" class="table table-bordered table-hover">
    <thead>
      <tr style="color:black;">
        <th>p_Product_type_id</th>
        <th>p_product_id</th>
        <th>p_Unit_price</th>
        <th>p_Quantity</th>
        </tr>
    </thead>
    <tbody>
      <tr class="group" *ngFor="let item of products">
        <td>{{item.p_Product_type_id}}</td>
        <td>{{item.p_product_id}}</td>
        <td>{{item.p_Unit_price}}</td>
        <td>{{item.p_Quantity}}</td>
      </tr>
    </tbody>
  </table>
  <br>
  <br>
  <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="total">Subtotal</label>
      <input formControlName="Subtotal" id="Subtotal" type="text" class="validate" [value]="total" [(ngModel)]="total">
    </div>
  </div>
  <hr>
  <br>
  <div id="add_homebox_button_container" class="row" style="float: right;">
    <button id="add_client_button" type="submit" class="btn waves-effect waves-light">
      Register
    </button>
</form>

Ts code: 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);
  }
}

Use [value]="total" and add the total to saved values when saving.使用[value]="total"并在保存时将总数添加到保存的值中。 You do not need to use the form to collect the values for saving.您不需要使用表单来收集值以进行保存。 You can handle this in your component.您可以在组件中处理此问题。

I'd say that using ngModel just to pass some value for saving is a bit hacky way.我会说使用ngModel只是为了传递一些保存值是一种有点hacky的方式。

I'd edit the onaddsale method to add the total to the sale.我会编辑onaddsale方法以将总数添加到销售中。 Then you could delete the total from the fb.group() and remove the formControlName="total" from template as well, since you do not really need that.然后,你可以删除totalfb.group()和删除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);
    ...
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度2 | 无法分配给数组,因为它是常量或只读属性 - Angular 2 | cannot assign to array because it is a constant or a read-only property 角度无法分配给“名称”,因为它是常量或只读属性 - Angular Cannot assign to 'name' because it is a constant or a read-only property 角度2 | 无法分配给变量,因为它是常量或只读属性 - Angular 2 | cannot assign to variable because it is a constant or a read-only property 错误 TS2540:无法分配给“i”,因为它是常量或只读属性 - error TS2540: Cannot assign to 'i' because it is a constant or a read-only property 无法分配给“值”,因为它是常量或只读属性。 Angular 6 - Cannot assign to 'value' because it is a constant or a read-only property. Angular 6 获取错误:&#39;无法分配到&#39;location&#39;,因为它是一个常量或只读属性&#39;与Angular app中的mailto函数 - Getting error: 'Cannot assign to 'location' because it is a constant or a read-only property' with mailto function in Angular app 无法分配给&#39;total&#39;,因为它是函数的常量或只读属性get total(){} - cannot assign to 'total' because it is a constant or a read only property for function get total(){} TS2540:无法分配给“矩阵”,因为它是只读属性 - TS2540: Cannot assign to 'matricule' because it is a read-only property “无法分配给 var,因为它是只读属性。” 使用 Jasmine 测试框架进行角度测试 - "Cannot assign to var because it is a read-only property." in angular test using Jasmine Test Framework 错误 TS2540:无法分配给“vfs”,因为它是只读属性 - error TS2540: Cannot assign to 'vfs' because it is a read-only property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM