feat(elements): reflect input property values to attributes - #70056
Open
Arul1998 wants to merge 1 commit into
Open
feat(elements): reflect input property values to attributes#70056Arul1998 wants to merge 1 commit into
Arul1998 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
reflectPropertiesoption onNgElementConfig. 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
truereflects as an empty attribute, whilefalse,nullandundefinedremove it. A guard prevents the reflected attribute change from being fed back throughattributeChangedCallback. The option defaults tofalse, 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?
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. Assigningan 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
reflectPropertiesoption onNgElementConfig. 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 stringtrue→ empty attribute;false/null/undefined→ attribute removed[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?
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-inputopt-in would also work if the team prefers. Happy to adjust based on review.