angular: bindToComponentInputs=true breaks default/initial value of field with @Input()

Which @angular/* package(s) are the source of the bug?

router or core

Is this a regression?

No

Description

given the following class:

export class SimpleComponent {
  @Input() readonly = true;   
}

and route: { path: '', component: SimpleComponent, pathMatch: 'full' },

the default value is undefined if bindToComponentInputs: true,

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/github-w4jhh2?file=src%2Fmain.ts

Please provide the exception or error you saw

readonly = - (default is true)

--- 

change line 53 to : false, then the default works again:
readonly = true - (default is true)

Please provide the environment you discovered this bug in (run ng version)

"@angular/animations": "17.0.3",
    "@angular/common": "^17.0.3",
    "@angular/compiler": "^17.0.3",
    "@angular/core": "^17.0.3",
    "@angular/platform-browser": "^17.0.3",
    "@angular/platform-browser-dynamic": "^17.0.3",
    "@angular/router": "^17.0.3",

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 24 (5 by maintainers)

Commits related to this issue

Most upvoted comments

the problem is that it breaks existing, working code when upgrading to router input binding and maybe a very unexpected thing (at least for me it was, luckily I found that before going to production)

Similar to this issue, an input signal with default value is also not working when using withComponentInputBinding().

title = input<string>('Default string');

Will not display the default string when no router params are provided. It would make more sense to me when values with an initial value would keep their initial value when no router params are available, and the current behaviour of setting the values to undefined would only apply to inputs without a default value.

Or maybe opt in for who can be changed by the router. I have a 7 classes that are both capable of being a route AND rendered inside other html, so most of the Inputs (in one case 15 out of 16 inputs) are not even expected to come from routing, but now all their defaults are undefined instead of various other expected values. Having only the one I WANT manipulated by the router actually be changed by it (by opting in) would be the easiest (at least in our use case).

I would have preferred to name it something different @RouterInput() or alike as it has different semantic

I don’t think we should have something like @RouterInput(). Because the advantage of the withComponentInputBinding configuration is that we can now create components that are independent of how they are consumed.

A component is fed by its @Input(). These inputs can come from a binding in a template or from the route configuration. The great thing is that developers can create components that do not depend on their execution context.

For the same reason, it’s a shame that default values are treated differently depending on the context (template binding or router).

Reopening and labeling as a documentation issue. The API docs should call this out and also mention that {required: true} does not apply in this situation either.

The only somewhat reasonable alternative to the current approach would be to not bind when the key is not present in the data, then assign undefined as it does today if an input key was present and then removed to avoid presenting stale information.

I’m not so sure myself that this is a good idea anymore (maybe the current implementation is the best…).

Because with this alternative, the same URL could display different results depending on the user location history.

Let’s demonstrate that.

Let’s take this component:

@Component({
  selector: 'app-product',
  template: `
    <h1>{{ products().title }}</h1>

    @if (!skipDetails()) {
      <p>{{ products().details }}</p>
    }
  `,
})
export class ProductComponent {
  product = input.required<Product>();

  skipDetails = input(true);
}

The route configuration has a resolver which takes the productId from the route params and returns the product:

export const routes: Routes = [
  {
    path: 'product/:productId',
    resolve: { product: productResolver },
    component: ProductComponent,
  }
];

If we visit the following URLs in this order :

http://localhost:4200/product/25

We see the product 25. And since the skipDetails input is not part of the router configuration, it retains its initial value (true).

!!! The details are skipped !!!

Then the user visits:

http://localhost:4200/product/26&skipDetails=false

The product 26 is displayed, but this time the details are not skipped.

Finally, the user clicks on his browser’s “back” button.

http://localhost:4200/product/25

The skipDetails input is again absent from the router and therefore receives now the value undefined (which is equivalent to false).

!!! The details are NOT skipped this time !!!


From a purely technical perspective, this is not reasonable.

Unless we introduce the following rules:

  • You should not rely on the default value for inputs that are intended to be exposed via the router.
  • For inputs that are not supposed to be exposed by the router you can rely on default values.
  • But remember that nothing prevents the user from manually modifying the URL to exploit these inputs.

It is still relevant to have the initial value untouched if the input is not part of the router configuration.

I understand @atscott’s arguments but I still think this behavior is a bug. Imagine we have working angular app, then if we toggle bindToComponentInputs ON then suddenly we get undefined in our inputs even though they are defined with initial value. This issue is then only noticed during runtime because from type perspective input has initial value, therefore it cannot be undefined.

I would also suggest to rather not reuse input() for route params but create something liker routeInput() that can always be undefined, also it will be clear it is meant for route params and we will not mix regular component inputs with route param inputs. Or other solution as was mentioned here https://github.com/angular/angular/issues/52946#issuecomment-2198484477, would be also acceptable. Or Angular should not reset values to undefined if such a route param doesn’t exist.

When using router input binding, the Router takes charge of binding all inputs for the component. The only somewhat reasonable alternative to the current approach would be to not bind when the key is not present in the data, then assign undefined as it does today if an input key was present and then removed to avoid presenting stale information. This still requires routes to handle missing inputs for the component.

I think that’s perfect! Because, depending on the functional use case, there are probably inputs that are not intended to be ever bound by the router. In this case, we simply want these inputs to be untouched by the router binding. They are typically updated by the component itself using a template event binding.

Setting up an input with an initial value, i.e.@Input() readonly = true;, is an initial value only to be used when the owner of the component has not (yet) bound a value. It is not a default value, but an initial value. Once the owner of the component chooses to bind to the input, there is no way to revert back to the initial value. This applies to templates as well: once a template binds a value, you cannot remove that binding to revert back to the initial value.

When using router input binding, the Router takes charge of binding all inputs for the component. The only somewhat reasonable alternative to the current approach would be to not bind when the key is not present in the data, then assign undefined as it does today if an input key was present and then removed to avoid presenting stale information. This still requires routes to handle missing inputs for the component.

Components used in routes with input binding enabled should either ensure the inputs are always present or the component should configure a default with an input transform. The documentation is being updated to mention this.

As I understand, when inputs are filled by router then initial values are not considered. When inputs are filled by anything else, then initial values make sense. Isn’t that strange?

btw this line on https://angular.io/guide/component-interaction shows that @Input may have a default value:

@Input('master') masterName = '';