0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

webcomponentsにAngularから値を渡す

Posted at

バージョン

Angular/core ~8.1.2

やりたかったこと

webcomponentsのディレクティブにAngularから値を渡す。

app.component.html

<clr-icon [shape]="item.icon"></clr-icon>

怒られ

普通にやると怒られが発生する。

VM47766 vendor.js:21971 Uncaught Error: Template parse errors:
Can't bind to 'shape' since it isn't a known property of 'clr-icon'.
1. If 'clr-icon' is an Angular component and it has 'shape' input, then verify that it is part of this module.
2. If 'clr-icon' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component

Angularの知らないプロパティを渡そうとしたからである。
だいたいの場合、moduleへ対象のAngular componentをexportしているmoduleをimportしていないことが原因で出るいつものやつだが、
今回は2.のケースに該当するため2.を実行する。

// app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  declarations: [AppComponent],
  imports: [
    CommonModule,
    RouterModule,
    ClarityModule
  ],
  schemas:[CUSTOM_ELEMENTS_SCHEMA] // これを追加
})
export class AppModule { }

アトリビュートの指定

これでエラーは出なくなるが、挙動的にどうもshapeアトリビュートに値が渡ってなさそうである。
調べたところ、どうやら[attr.アトリビュート名]で指定する必要があるそうだ。

正解は以下の通り。

<clr-icon [attr.shape]="item.icon"></clr-icon>

出典

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?