-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Description
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No - model()
is relatively new and in developer preview
Description
A model()
property provides an input and an output binding; but when the input is bound to a normal object (not a signal) no change is emitted.
Given a component:
@Component({
selector: 'test-model',
template: '<ng-content></ng-content>'
})
class ModelEmitComponent {
readonly model = model<number | null>(null);
// Set of all non-null emitted values.
readonly emitted: number[] = [];
@Input()
set setModelValue(v: number) {
this.model.set(v);
}
constructor() {
this.model.subscribe(v => {
if (v !== null) {
this.emitted.push(v);
}
})
}
}
when the model
object is set via the old-school @Input
and WritableSignal.set()
, like:
<test-model [setModelValue]="3" />
The value change is emitted, so a change listener can see it.
When the model
object is set using model
input binding:
<test-model [model]="3" />
The value change is not emitted, so there is no way to know that a change occurred. I'm pretty confident in saying that developers will expect all model
changes to be emitted, not just those caused by WriteableSignal.set()
.
Please provide a link to a minimal reproduction of the bug
https://github.com/johncrim/ng18-tests/blob/main/projects/output-emit/src/app/model-emit.spec.ts
Please provide the exception or error you saw
No exception; the error is that the change event is not emitted.
Please provide the environment you discovered this bug in (run ng version
)
Angular CLI: 18.2.7
Node: 22.6.0
Package Manager: yarn 4.5.0
OS: win32 x64
Angular: 18.2.7
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
@angular-devkit/architect 0.1802.7
@angular-devkit/build-angular 18.2.7
@angular-devkit/core 18.2.7
@angular-devkit/schematics 18.2.7
@schematics/angular 18.2.7
rxjs 7.8.1
typescript 5.5.4
zone.js 0.14.10
Anything else?
Test cases show that the model binding failing to emit occurs both for initial binding, and for input changes after the component is created.
describe('model() emit', () => {
let spectator: SpectatorHost<ModelEmitComponent>;
const createHost = createHostFactory(ModelEmitComponent);
it('@Input model.set() is emitted', () => {
spectator = createHost(`<test-model [setModelValue]="3" />`);
expect(spectator.component.model()).toBe(3);
expect(spectator.component.emitted).toEqual([3]);
});
it('model input is emitted (fails)', () => {
spectator = createHost(`<test-model [model]="3" />`);
expect(spectator.component.model()).toBe(3);
expect(spectator.component.emitted).toEqual([3]); // fails b/c model binding to regular object doesn't emit
});
it('@Input model.set() emits when changed after create', () => {
spectator = createHost(`<test-model [setModelValue]="initialValue" />`, {
hostProps: {
initialValue: null
}
});
expect(spectator.component.model()).toBe(null);
expect(spectator.component.emitted).toEqual([]);
spectator.setHostInput('initialValue', 2);
expect(spectator.component.model()).toBe(2);
expect(spectator.component.emitted).toEqual([2]);
});
it('model input emits when changed after create (fails)', () => {
spectator = createHost(`<test-model [model]="initialValue" />`, {
hostProps: {
initialValue: null
}
});
expect(spectator.component.model()).toBe(null);
expect(spectator.component.emitted).toEqual([]);
spectator.setHostInput('initialValue', 2);
expect(spectator.component.model()).toBe(2);
expect(spectator.component.emitted).toEqual([2]); // fails b/c model binding to regular object doesn't emit
});
});