{ "id": "guide/migration-dynamic-flag", "title": "Dynamic queries flag migration", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Dynamic queries flag migrationlink

\n

What does this migration do?link

\n

In Angular version 8, a schematic added static flags to all @ViewChild() and @ContentChild() queries.\nThis was the first step towards changing the default behavior.\nWith version 9, the default value changes to static: false and the flag becomes optional.

\n

This schematic scans classes in the compilation and for each class, checks if the members have a @ViewChild() or @ContentChild() query with the static flag set to false.\nIf so, the schematic removes the flag, as it now matches the default.

\n

Before:

\n\n@ViewChild('foo', {static: false}) foo: ElementRef;\n\n@ViewChild('bar', {static: true}) bar: ElementRef;\n\n

After:

\n\n@ViewChild('foo') foo: ElementRef;\n\n// this query doesn't change because the static value is true\n@ViewChild('bar', {static: true}) bar: ElementRef;\n\n

Note that the flag is not supported in @ViewChildren() or @ContentChildren() queries, so the schematic will not check these properties.

\n

Why is this migration necessary?link

\n

This schematic performs a code cleanup to remove static flags that match the default, as they are no longer necessary.\nFunctionally, the code change should be a noop.

\n

Before version 9, Angular figured out the static or dynamic nature of a query automatically, based on how the template was written.\nLooking at templates in this way, however, caused buggy and surprising behavior (see more about that in the Static Query Migration Guide).\nAs of version 9, Angular uses dynamic queries (static: false) by default, which simplifies queries.\nDevelopers can still explicitly set a query to static: true if necessary.

\n
\n

What is the difference between static and dynamic queries?link

\n

The static option for @ViewChild() and @ContentChild() queries determines when the query results become available.

\n

With static queries (static: true), the query resolves once the view has been created, but before change detection runs.\nThe result, though, will never be updated to reflect changes to your view, such as changes to ngIf and ngFor blocks.

\n

With dynamic queries (static: false), the query resolves after either ngAfterViewInit() or ngAfterContentInit() for @ViewChild() and @ContentChild() respectively.\nThe result will be updated for changes to your view, such as changes to ngIf and ngFor blocks.

\n

For more information, see the following entries in the\nStatic Query Migration Guide:

\n\n
\n

What does this mean for libraries?link

\n

In order to support applications that are still running with version 8, the safest option for libraries is to retain the static flag to keep the resolution timing consistent.

\n\n

What about applications using non-migrated libraries?link

\n

Because this is a code cleanup that is a noop, non-migrated libraries will work the same either way.

\n\n \n
\n\n\n" }