Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions 7 packages/elements/src/create-custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {Injector, Type} from '@angular/core';
import {Injector, Type, isSignal} from '@angular/core';
import {Subscription} from 'rxjs';

import {ComponentNgElementStrategyFactory} from './component-factory-strategy';
Expand Down Expand Up @@ -237,10 +237,11 @@ export function createCustomElement<P>(
}

// Add getters and setters to the prototype for each property input.
inputs.forEach(({propName, transform}) => {
inputs.forEach(({propName, transform, isSignal: _isSignal}) => {
Object.defineProperty(NgElementImpl.prototype, propName, {
get(): any {
return this.ngElementStrategy.getInputValue(propName);
const inputValue = this.ngElementStrategy.getInputValue(propName);
return _isSignal && isSignal(inputValue) ? inputValue() : inputValue;
},
set(newValue: any): void {
this.ngElementStrategy.setInputValue(propName, newValue, transform);
Expand Down
47 changes: 46 additions & 1 deletion 47 packages/elements/test/create-custom-element_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {
Injector,
input,
Input,
isSignal,
NgModule,
Output,
signal,
WritableSignal,
} from '@angular/core';
import {BrowserModule, platformBrowser} from '@angular/platform-browser';
import {Subject} from 'rxjs';
Expand Down Expand Up @@ -316,6 +319,29 @@ describe('createCustomElement', () => {
expect(strategy.inputs.get('fooSignal')).toBe('value-signal');
});

it('should return value from input getter for input signal', () => {
const {selector, ElementCtor} = createTestCustomElementForSignal();
const element = document.createElement(selector) as HTMLElement & {
fooSignal: string | null;
};
element.setAttribute('foo-signal', 'value-signal');

customElements.define(selector, ElementCtor);
testContainer.appendChild(element);
expect(element.fooSignal).toBe('value-signal');
});

it('should not unpack signal value with input decorator having signal as value', () => {
const {selector, ElementCtor} = createTestCustomElementForSignal();
const element = document.createElement(selector) as HTMLElement & {
fooFoo: WritableSignal<string | null>;
};

customElements.define(selector, ElementCtor);
testContainer.appendChild(element);
expect(isSignal(element.fooFoo)).toBe(true);
});

// Helpers
function createAndRegisterTestCustomElement(strategyFactory: NgElementStrategyFactory) {
const {selector, ElementCtor} = createTestCustomElement(strategyFactory);
Expand All @@ -332,6 +358,13 @@ describe('createCustomElement', () => {
};
}

function createTestCustomElementForSignal() {
return {
selector: `test-element-${++selectorUid}`,
ElementCtor: createCustomElement(TestSignalComponent, {injector}),
};
}

@Component({
selector: 'test-component',
template: 'TestComponent|foo({{ fooFoo }})|bar({{ barBar }})',
Expand All @@ -349,9 +382,21 @@ describe('createCustomElement', () => {
@Output() bazBaz = new EventEmitter<boolean>();
@Output('quxqux') quxQux = new EventEmitter<Object>();
}

@Component({
selector: 'test-signal-component',
template: 'TestSignalComponent|foo({{ fooFoo() }})|signal({{ fooSignal() }})',
standalone: false,
})
class TestSignalComponent {
@Input() fooFoo = signal<string | null>(null);
// This needs to apply the decorator and pass `isSignal`, because
// the compiler transform doesn't run against JIT tests.
@Input({isSignal: true} as Input) fooSignal = input<string | null>(null);
}
@NgModule({
imports: [BrowserModule],
declarations: [TestComponent],
declarations: [TestComponent, TestSignalComponent],
})
class TestModule implements DoBootstrap {
ngDoBootstrap() {}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.