Skip to content

feat(elements): reflect input property values to attributes - #70056

Open
Arul1998 wants to merge 1 commit into
angular:mainfrom
Arul1998:feat/elements-reflect-properties
Open

feat(elements): reflect input property values to attributes#70056
Arul1998 wants to merge 1 commit into
angular:mainfrom
Arul1998:feat/elements-reflect-properties

Conversation

@Arul1998

@Arul1998 Arul1998 commented Aug 3, 2026

Copy link
Copy Markdown

Angular Elements syncs attributes to input properties, but not the reverse: assigning an input property never updated the corresponding attribute. This adds an opt-in reflectProperties option on NgElementConfig. When enabled, assigning a primitive value (string, number or boolean) to an input property writes it back to the matching attribute so the two stay in sync.

Non-primitive values are intentionally not reflected, since they cannot be meaningfully serialized to an attribute. A boolean true reflects as an empty attribute, while false, null and undefined remove it. A guard prevents the reflected attribute change from being fed back through attributeChangedCallback. The option defaults to false, preserving existing behavior.

Fixes #23557

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Angular Elements keeps input properties in sync when an attribute changes
(setAttribute(...) updates the property), but not the other way around. Assigning
an input property never updates the corresponding attribute, so the property and
attribute drift out of sync.

Issue Number: #23557

What is the new behavior?

Adds an opt-in reflectProperties option on NgElementConfig. When enabled,
assigning a primitive value to an input property reflects it back to the matching
attribute so the two stay in sync:

  • string / number → written to the attribute as a string
  • true → empty attribute; false / null / undefined → attribute removed
  • objects / arrays / functions are not reflected, avoiding meaningless
    [Object object] attributes (the concern raised earlier on the issue)

A guard prevents the reflected attribute change from being fed back through
attributeChangedCallback. Unit tests cover string, number, boolean, null/undefined,
non-primitive, aliased-attribute, and no-feedback-loop cases.

Does this PR introduce a breaking change?

  • Yes
  • No

The option defaults to false, so existing behavior is unchanged.

Other information

Open to shaping the API — the opt-in flag lives on NgElementConfig, but a per-input
opt-in would also work if the team prefers. Happy to adjust based on review.

Angular Elements syncs attributes to input properties, but not the
reverse: assigning an input property never updated the corresponding
attribute. This adds an opt-in `reflectProperties` option on
`NgElementConfig`. When enabled, assigning a primitive value (string,
number or boolean) to an input property writes it back to the matching
attribute so the two stay in sync.

Non-primitive values are intentionally not reflected, since they cannot
be meaningfully serialized to an attribute. A boolean `true` reflects as
an empty attribute, while `false`, `null` and `undefined` remove it. A
guard prevents the reflected attribute change from being fed back through
`attributeChangedCallback`. The option defaults to `false`, preserving
existing behavior.

Fixes angular#23557
@pullapprove
pullapprove Bot requested a review from kirjs August 3, 2026 13:57
@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: elements Issues related to Angular Elements labels Aug 3, 2026
@ngbot ngbot Bot added this to the Backlog milestone Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: elements Issues related to Angular Elements detected: feature PR contains a feature commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Angular Elements should keep an HTML attribute value in sync with a property value

1 participant