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.

DebugElement.queryAll can return results in different order than DOM.

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior DebugElements are returned from DebugElement.queryAll() in the same order they were first created in the ViewContainer regardless if they were moved by a structural directive like NgFor. If they were moved, then the order is out of sync with with the DOM.

Expected behavior DebugElements should be returned from a query in the order they appear within the DOM. Order should be updated when underlying ViewRefs are moved around in a ViewContainer

Minimal reproduction of the problem with instructions Plunker Reproducing the Issue

Running plunker will run tests that are almost identical to the unit tests for NgFor. Tests illustrate how adding and removing ViewRefs from a ViewContainer work as expected. Two tests illustrate how moving ViewRefs and using DebugElement to query for the moved items fails. Two tests illustrate how the DOM is in the correct order by using the native querySelectorAll() method.

What is the motivation / use case for changing the behavior? DebugElements should be returned in the order they appear in the DOM when queried for.

Please tell us about your environment: Windows 10 x64

  • Angular version: 2.2.1 with angular-cli 1.0.0-beta.20-4 (plunker not at this version)

  • Browser: all

  • Language: Typescript

  • Node (for AoT issues): node --version = 7.1.0

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jshoudy11commented, Jun 6, 2018

No clean solution but I found a workaround. Something like:

const debugEls = element.queryAll(‘foo’); const domEls = Array.from(element.nativeElement.querySelectorAll(selector)); debugEls.sort((a: DebugElement, b: DebugElement) => { return domEls.indexOf(a.nativeElement) - domEls.indexOf(b.nativeElement); }); return debugEls;

0reactions
amitpchigadanicommented, Jun 7, 2018

Yes, Thanks @jshoudy11 , We can verify our view template directly by fetching DOM elements instead of DebugElements. That’s a nice alternative as of now. Hope this issue resolves sooner.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - DebugElement.children has a different order than ...
I have an html table and want to get all rows (s). However calling .children on a DebugElement or a NativeElement returns a...
Read more >
DebugElement - Angular
A map containing the class names on the element as keys. This map is derived from the className property of the DOM element....
Read more >
Getting to know fixture.debugElement : Angular Unit Testing
querySelector('#shan'). nativeElement returns a reference to the DOM element which can also come under debugElement as stated above.You can use it do actions ......
Read more >
Testing Components – Testing Angular
We will learn more on testing Inputs and Outputs later. DebugElement. For accessing elements in the DOM, Angular has another abstraction: ...
Read more >
Component testing scenarios - Angular
You'll write a sequence of tests that inspect the value of the <h1> element that ... Rather than wonder when the test fixture...
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