question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input parameter order does matter

See original GitHub issue

After updating from 8th to the 10th version, the input parameter order does matter.

šŸž bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

Yes, the previous version in which this bug was not present was: 8th version

Description

In case the Input decorator is used on a setter and a parallel input parameter is used inside the setter, this parallel parameter is not available during the setter execution.

šŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-ivy-parameter-order-bug?file=src%2Fapp%2Fapp.component.html

šŸ”„ Exception or Error

Error: cannot read property 'bla' of undefined



šŸŒ Your Environment

Angular Version:

10.2.3



Anything else relevant?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
alxhubcommented, Dec 10, 2020

It seems that in ViewEngine (i.e. Angular v8) the inputs were evaluated in alphabetic order.

This isn’t actually the case. I think there’s an issue with your stackblitz examples. The a setter is defined as:

  @Input() set a(v) {
    this.text = `A: ${this.a}; B: ${v}`;
  }

this.text includes two values here, both of which are values for a (unlike what the B: label implies). Really it’s showing the value before and after the binding is evaluated, which will predictably be undefined and then the singular incoming value.

In actuality, in View Engine bindings were set in the order of @Input field declaration in the directive/component being consumed. This wasn’t a chosen or specified behavior, merely an implementation detail of how the inputs were captured when iterating members of the directive class.

Despite being rather arbitrary, this ordering does intuitively make a certain amount of sense. It’s stable and well-defined, and doesn’t vary from consumer to consumer. If one input’s setter consumes the value of another input, developers tend to naturally write the declaration for the dependency first, and then its consumer. A lot of the time then, this approach just ā€œworkedā€.

The Ivy design initially leaned towards following the ordering at the template usage site instead. This was favored for two reasons:

  1. Ivy’s locality principle meant that knowledge of the declaration ordering of bindings wasn’t available, meaning that VE’s behavior could not be emulated.
  2. Another general principle was to treat the template as code, and evaluate it start-to-finish exactly as the user wrote it. A major goal was to have source mapping of templates good enough such that one could step through the HTML in a debugger, and it’s much more natural to jump through bindings in the left-to-right order that they’re written.

In practice, this constraint proved overly restrictive. We realized it would limit our ability to optimize templates down the road, and decided that like View Engine, Ivy would not commit to a particular import binding order. I believe the decision at the time was:

  • From element to element in the template, bindings will always be evaluated in template order. That is, when the template says:
<foo [a]="..."></foo>
<bar [b]="..."></bar>`

The [a] binding will always be evaluated/set before the [b] binding.

  • Within an element, the order is unspecified. The template compiler is free to reorder bindings as it sees fit. For example, in the template:
<foo [a]="..." b="..." [c]="..."></foo>

The template compiler may evaluate both [a] and [c] before b, in order to chain the binding instructions and generate a smaller template:

property('a', ...)('c', ...);
attribute('b', ...);
2reactions
dopusteamcommented, Dec 7, 2020

Assuming that this is not an expected regression (I don’t see it in the breaking changes for ivy), then it could be resolved in the Ivy compiler by sorting inputs before generating instructions, I believe.

Well, I hope nothing changed since this comment 😃

To answer your question, we don’t consider input setting order within directives to be public API. If you want to react to a directive’s inputs in a certain order, using the ngOnChanges lifecycle hook tends to be a better place. As such, I’m closing this issue as working as intended.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the order of parameters matter? - python
The only way to know what value you meant to pass for each parameter is by using the correct order. For python, you...
Read more >
Name of Property: The Order Of Parameters in a Function ...
As the title says: I have a function where the order of parameters does not matter. An example would be ADD : ADD(1,2)...
Read more >
Does order of parameters matter? - C++ Forum
Nope, order does not matter. No there is no set rule about the order of parameters, however you should strive to keep the...
Read more >
Why does the order of the arguments matter? - Python FAQ
It doesn't matter what order they are given in the parameter so long as the arguments in the call expression match the correct...
Read more >
Change the order of a paginated report parameter
Change the order of paginated report parameters when you have a dependent parameter that is listed before the parameter it is dependent on....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found