angular-cn/packages
Kara Erickson 19afb791b4 feat(core): allow users to define timing of ViewChild/ContentChild queries (#28810)
Prior to this commit, the timing of `ViewChild`/`ContentChild` query
resolution depended on the results of each query. If any results
for a particular query were nested inside embedded views (e.g.
*ngIfs), that query would be resolved after change detection ran.
Otherwise, the query would be resolved as soon as nodes were created.

This inconsistency in resolution timing had the potential to cause
confusion because query results would sometimes be available in
ngOnInit, but sometimes wouldn't be available until ngAfterContentInit
or ngAfterViewInit. Code depending on a query result could suddenly
stop working as soon as an *ngIf or an *ngFor was added to the template.

With this commit, users can dictate when they want a particular
`ViewChild` or `ContentChild` query to be resolved with the `static`
flag. For example, one can mark a particular query as `static: false`
to ensure change detection always runs before its results are set:

```ts
@ContentChild('foo', {static: false}) foo !: ElementRef;
```

This means that even if there isn't a query result wrapped in an
*ngIf or an *ngFor now, adding one to the template later won't change
the timing of the query resolution and potentially break your component.

Similarly, if you know that your query needs to be resolved earlier
(e.g. you need results in an ngOnInit hook), you can mark it as
`static: true`.

```ts
@ViewChild(TemplateRef, {static: true}) foo !: TemplateRef;
```

Note: this means that your component will not support *ngIf results.

If you do not supply a `static` option when creating your `ViewChild` or
`ContentChild` query, the default query resolution timing will kick in.

Note: This new option only applies to `ViewChild` and `ContentChild`
queries, not `ViewChildren` or `ContentChildren` queries, as those types
already resolve after CD runs.

PR Close #28810
2019-02-19 12:56:25 -08:00
..
animations build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
bazel feat(compiler-cli): make enableIvy ngtsc/true equivalent (#28616) 2019-02-19 12:28:44 -08:00
benchpress feat: optionally save complete performance log in chrome benchpress tests (#27551) 2019-02-05 23:35:32 -05:00
common docs: incorporate review feedback (#26827) 2019-02-14 19:33:14 +00:00
compiler feat(core): allow users to define timing of ViewChild/ContentChild queries (#28810) 2019-02-19 12:56:25 -08:00
compiler-cli build(compiler-cli): upgrade chokidar to latest version (#28797) 2019-02-19 12:54:11 -08:00
core feat(core): allow users to define timing of ViewChild/ContentChild queries (#28810) 2019-02-19 12:56:25 -08:00
docs
elements build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
examples build(bazel): specify tsconfig-test dependency (#28696) 2019-02-16 20:57:34 -08:00
forms build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
http build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
language-service build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
platform-browser docs(platform-browser): fix a grammatical error in the BrowserModule documentation (#28808) 2019-02-19 12:54:52 -08:00
platform-browser-dynamic build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
platform-server build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
platform-webworker build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
platform-webworker-dynamic build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
private/testing build: run playground e2e examples with bazel (#28490) 2019-02-04 16:51:11 -05:00
router build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
service-worker build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
upgrade build: remove unused rollup.config.js files (#28646) 2019-02-14 19:28:08 +00:00
BUILD.bazel build(bazel): specify tsconfig-test dependency (#28696) 2019-02-16 20:57:34 -08:00
README.md
empty.ts
goog.d.ts
license-banner.txt
system.d.ts
tsconfig-build.json
tsconfig-test.json
tsconfig.json build: switch example e2e tests to bazel (#28402) 2019-01-28 19:21:09 -08:00
types.d.ts build: switch to typescript's es2015 typings (#28570) 2019-02-06 14:17:55 -05:00

README.md

Angular

The sources for this package are in the main Angular repo. Please file issues and pull requests against that repo.

License: MIT