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

Problem

When compiling your Angular application, you see an error like this:

✘ [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',
         ╵                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Solution

This error occurs when you try to bind a property that Angular doesn’t know about.

This is a data-... attribute so Angular does not need to know about it specifically. But in order to avoid tripping the type checker, use attr.data-sensor instead:

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

This way, Angular knows that this is an attribute binding and not a property binding.