简体   繁体   中英

Angular: TS2322: Type 'number' is not assignable to type 'string'

Hi have the following bellow code in my angular application. When I try run my production build command I get the following error:

Error TS2322: Type 'number' is not assignable to type 'string'.

I have tried changing to:

[value]="'1'"

however if I do this it won't check the box


          <label mdbLabel class="form-label mt-3 mb-0">Time Units </label>
          <mdb-form-control>
            <div class="form-check form-check-inline">
              <input
                mdbRadio
                class="form-check-input"
                type="radio"
                mdbInput formControlName="timeUnitHHMM"
                id="timeUnitHHMM"
                [value]="1"
                [checked]="validationForm.getRawValue()?.timeUnitHHMM == 1"
              />
              <label class="form-check-label" for="timeUnitHHMM">HHMM</label>
            </div>


    this.validationForm = new FormGroup({
      timeUnitHHMM: new FormControl(1),
    });

    // @ts-ignore
    this.validationForm.get('timeUnitHHMM').valueChanges.subscribe(value => {
      if (value === 1) {
        this.validationForm.patchValue({timeUnitDecimal: 0});

      }
    });


Wondering if anyone has any solution.

Thanks

[value]="1" define string-binding. So 1 comes as a string.

If you want to transfer the 1 as a number, the simplest way is to force the cast from string to number with a + infront of the 1

[value]="+1"

However, I recommend that you take a look at the angular data bindings. Under the following link you will find the official documentation:

Data-binding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM