0

I really don't understand what's the problem is with this code

<input #quantity type="number" matInput formControlName="quantity" (input)="onQuantity($event, i)" placeholder="Quantity"/>

onQuantity(event: InputEvent, i: number) {
    if (!this.totals[i]) {
      this.totals[i] = { price: '0', quantity: 1 };
    }
    let value = (event.target as HTMLInputElement).value;
    value = value ? value : '0';
    this.totals[i].quantity = parseInt(value, 10);
  }

The compiler get an error Argument of type 'Event' is not assignable to parameter of type 'InputEvent'. and if I use

onQuantity(event: Event, i: number)

I get Type 'Event' is missing the following properties from type 'InputEvent': data, inputType, isComposing, detail, and 2 more.

so I don't known what way to turn :( Can anyone help me, please?

UPDATE

I have worked it out with

(input)="onQuantity(quantity.value, i)"

https://angular.io/guide/user-input

user3887366
  • 2,226
  • 4
  • 28
  • 41

3 Answers3

1

use (input)="onQuantity($any($event), i)" in html template

  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Apr 26 '22 at 21:44
0

change event belongs to InputEvent type interface

Try this:

onQuantity(event: InputEvent, i: number){}
Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
  • thanks for the reply but was my first try :( and give me Argument of type 'Event' is not assignable to parameter of type 'InputEvent'. – user3887366 May 14 '20 at 09:09
  • Do you have both "fullTemplateTypeCheck and "strictDomEventTypes" are enabled in ts config? – Chellappan வ May 14 '20 at 09:45
  • I've got strictTemplates= true includes both – user3887366 May 14 '20 at 13:11
  • If you disbale strictDomEventTypes to false then it will not throw any error or else you have to wrap event to any type like this in template (input)="onQuantity($any($event), i)" – Chellappan வ May 14 '20 at 13:14
  • I know but the policy is strictTemplates= true :) – user3887366 May 14 '20 at 13:59
  • This is what they have mentioned in the documentation you can file an issue: – Chellappan வ May 14 '20 at 14:03
  • A type-checking error that you cannot resolve with any of the recommended methods can be the result of a bug in the template type-checker itself. If you get errors that require falling back to basic mode, it is likely to be such a bug. If this happens, please file an issue so the team can address it. – Chellappan வ May 14 '20 at 14:03
  • Check this:https://angular.io/guide/template-typecheck#troubleshooting-template-errors – Chellappan வ May 14 '20 at 14:04
0

Please use this

onQuantity(event:InputEvent, i: number){
    // do your operation
  }

the type of event is InputEvent. You can check it by using console.log(event)

For me it gives

1

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24