Jak opravit Angular Can't bind to 'data-...' since it isn't a known property of ...

Problém

Při kompilaci vaší Angular aplikace vidíte chybu jako tato:

angular_error_output.txt
✘ [ERROR] NG8002: Can't bind to 'data-sensor' since it isn't a known property of 'div'. [plugin angular-compiler]

    src/app/my-component/my-component.component.html:107:39:
      107 │ ...                <div class="sensor" [data-sensor]="sensor | json">
          ╵                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  Error occurs in the template of component NoxecoDepolMeasurementDisplayComponent.

    src/app/my-component/my-component.component.ts:31:17:
      31 │     templateUrl: './my-component.component.html',
         ╵                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Řešení

Tato chyba se objevuje, když se pokoušíte bindovat property, kterou Angular nezná.

To je data-... atribut, takže Angular nemusí o něm specificky vědět. Ale pro avoidance tripping type checkeru použijte attr.data-sensor místo toho:

my-component.component.html
<div class="sensor" [attr.data-sensor]="sensor | json">

Tímto způsobem Angular ví, že to je attribute binding a ne property binding.


Podívejte se na podobné články podle kategorie: Angular Typescript