Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Input properties not initializing when using ViewEncapsulation.ShadowDom #36642

Closed
fireflysemantics opened this issue Apr 15, 2020 · 3 comments
Labels
area: elements Issues related to Angular Elements
Milestone

Comments

@fireflysemantics
Copy link

Did a writeup with Demo Projects here:

https://stackoverflow.com/questions/61225191/angular-custom-element-input-property-not-initializing?noredirect=1#comment108312318_61225191

Tried switching the demo app to ViewEncapsulation.Emulated and that also does not work.

@alan-agius4 alan-agius4 transferred this issue from angular/angular-cli Apr 15, 2020
@atscott atscott added the area: core Issues related to the framework runtime label Apr 15, 2020
@ngbot ngbot bot added this to the needsTriage milestone Apr 15, 2020
@pkozlowski-opensource
Copy link
Member

@pkozlowski-opensource pkozlowski-opensource added area: elements Issues related to Angular Elements and removed area: core Issues related to the framework runtime labels Apr 16, 2020
@gkalpak
Copy link
Member

gkalpak commented Jul 13, 2020

The reason this is not working is that gistId="foo" will update the element's HTML attribute, unless it knows the upderlying component has a gistId property. When using a Custom Element, Angular treats it as a regular HTML element (it doesn't know anything about the underlying Angular component), so it will just update the element's gistId attribute (and because HTML is case-insensitive the gistid attribute (all lowercase)).

There are a number of ways to fix this, including:

  1. Since this is a common scenario, @angular/elements will automatically map camelCased inputs to the equivalent kebab-cased attributes. So, the component input associated with the gistId property will be set from the value of the gist-id attribute. Thus, when consuming the component as a Custom Element, you can instead use:

    -<fs-gist gistId="some-value"></fs-gist>
    +<fs-gist gist-id="some-value"></fs-gist>

    StackBlitz demo 1

  2. You can explicitly tell Angular to bind to a property on the element using the [<propName>]="<expression>" syntax. When using @angular/elements, HTML element properties that correspond to component inputs will be automatically passed through to the component. So, you can do:

    -<fs-gist gistId="some-value"></fs-gist>
    +<fs-gist [gistId]="'some-value'"></fs-gist>

    StackBlitz demo 2

    Notice that 'some-value' is wrapped in single-quotes, because [prop]="..." syntax expects an expression.
    Here, the expression is 'some-value', which evaluates to the string with content some-value.

Based on the above, I am going to close this issue, since everything seems to work as intended.

@gkalpak gkalpak closed this as completed Jul 13, 2020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: elements Issues related to Angular Elements
Projects
None yet
Development

No branches or pull requests

4 participants