I wanted to ask this in the Angular's project Github, but apparently questions/clarifications belongs to StackOverflow, so here we are :)
Lib authors can implement two-way binding using the new model() function. By using this function, Angular automatically creates a corresponding output for that model, ultimately making possible to use the [property] and (propertyChange) pattern. However, the component immediately emits the default/initial value through the output event as soon as it's initialized.
For example, with this third-party component:
@Component(...)
export class ThirdPartyChildComponent {
value = model.required<string>();
}
And using it like this:
@Component({
template: `<third-party-child [value]="defaultValue()" (valueChange)="logValueChange($event)">`
})
export class ParentComponent {
public defaultValue = input('initial value');
public logValueChange(value: string) {
console.log(value);
}
}
The logValueChange method is called immediately with 'initial value' during initialization.
Expected Behavior
As a consumer, I would expect the output event (valueChange) to only emit when the value actually changes after initialization, not immediately upon receiving the initial input value.
In this stackblitz this initial emission doesn't occur in the ngModel, Angular Forms or even Html Inputs.
Technically speaking, I do understand why by using the model() this might happen, but I'm trying to determine if:
- This is a design issue with the third-party component
- Perhaps related to a version specific issue?
- Should library authors provide separate mechanisms for initial values vs. change events?
- It's on my end as a consumer to differentiate between the first emission and future emissions