fix(devtools): stop relying on getAllAngularRootElements in Angular DevTools' backend code. (#45983)

With the introduction of standalone components, it is no longer guaranteed that getAllAngularRootElements will be available on the global object. This PR removes the dependency on this function so that DevTools can continue to work for Angular applications that use `bootstrapApplication`.

PR Close #45983
This commit is contained in:
AleksanderBodurri
2022-05-13 00:25:20 -04:00
committed by Jessica Janiuk
parent a4deac3fda
commit 3a468813ca
3 changed files with 8 additions and 9 deletions
@@ -68,13 +68,12 @@ describe('angular-check', () => {
});
it('should recognize Ivy apps', () => {
(window as any).getAllAngularRootElements = (): Element[] => {
const el = document.createElement('div');
(el as any).__ngContext__ = 0;
return [el];
};
const el = document.createElement('div');
el.setAttribute('ng-version', '0.0.0-PLACEHOLDER');
(el as any).__ngContext__ = 0;
document.body.append(el);
expect(appIsAngularIvy()).toBeTrue();
delete (window as any).getAllAngularRootElements;
el.remove();
});
});
@@ -13,7 +13,8 @@ export const appIsAngularInDevMode = (): boolean => {
};
export const appIsAngularIvy = (): boolean => {
return typeof (window as any).getAllAngularRootElements?.()?.[0]?.__ngContext__ !== 'undefined';
const rootElement = (window as any).document.querySelector('[ng-version]');
return typeof rootElement?.__ngContext__ !== 'undefined';
};
export const appIsAngular = (): boolean => {
@@ -46,8 +46,7 @@ function detectAngular(win: Window): void {
{
// Needs to be inline because we're stringifying
// this function and executing it with eval.
isIvy: typeof (window as any).getAllAngularRootElements?.()?.[0]?.__ngContext__ !==
'undefined',
isIvy: typeof (ngVersionElement as any)?.__ngContext__ !== 'undefined',
isAngular,
isDebugMode,
isSupportedAngularVersion,