|
| 1 | +## ViewChild Static attribute ([link](https://angular.io/api/core/ViewChild)) |
| 2 | + |
| 3 | +- If static is set to False, angular will use [ɵɵstaticViewQuery](https://github.com/angular/angular/blob/698b0288bee60b8c5926148b79b5b93f454098db/packages/core/src/render3/query.ts#L456) (Creates new QueryList for a static view query). |
| 4 | + |
| 5 | +- If static is set to True, Angular will use [ɵɵviewQuery](https://github.com/angular/angular/blob/698b0288bee60b8c5926148b79b5b93f454098db/packages/core/src/render3/query.ts#L470) (Creates new QueryList, stores the reference in LView and returns QueryList). |
| 6 | + |
| 7 | +When Angular will call [ɵɵqueryRefresh](https://github.com/angular/angular/blob/698b0288bee60b8c5926148b79b5b93f454098db/packages/core/src/render3/query.ts#L431) method (*Refreshes a query by combining matches from all active views and removing matches from deleted views*) and if the query is static, it will be never refreshed after the creation of the view (improve performance), but now if the query is not static it will be refreshed each time (performance impact). |
| 8 | + |
| 9 | +## Output compiler example |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +```javascript |
| 15 | +// Static is set to True |
| 16 | +const _c0 = ["uc1"]; |
| 17 | +const _c1 = ["uc2"]; |
| 18 | +function Example4Component_div_0_Template(rf, ctx) { if (rf & 1) { |
| 19 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementStart"](0, "div", null, 1); |
| 20 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementStart"](2, "p"); |
| 21 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵtext"](3, "My amazing content !"); |
| 22 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementEnd"](); |
| 23 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementEnd"](); |
| 24 | +} } |
| 25 | +class Example4Component extends _app_example1_a_component__WEBPACK_IMPORTED_MODULE_1__["AComponent"] { |
| 26 | + constructor() { |
| 27 | + super(); |
| 28 | + // this.printRef.push(() => this.uc1); |
| 29 | + this.printRef.push(() => this.uc2); |
| 30 | + } |
| 31 | +} |
| 32 | +Example4Component.ɵfac = function Example4Component_Factory(t) { return new (t || Example4Component)(); }; |
| 33 | +Example4Component.ɵcmp = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵdefineComponent"]({ type: Example4Component, selectors: [["app-example4"]], viewQuery: function Example4Component_Query(rf, ctx) { if (rf & 1) { |
| 34 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵstaticViewQuery"](_c0, true); |
| 35 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵstaticViewQuery"](_c1, true); |
| 36 | + } if (rf & 2) { |
| 37 | + var _t; |
| 38 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵqueryRefresh"](_t = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵloadQuery"]()) && (ctx.uc1 = _t.first); |
| 39 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵqueryRefresh"](_t = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵloadQuery"]()) && (ctx.uc2 = _t.first); |
| 40 | + } }, features: [_angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵInheritDefinitionFeature"]], decls: 1, vars: 1, consts: [[4, "ngIf"], ["uc2", ""]], template: function Example4Component_Template(rf, ctx) { if (rf & 1) { |
| 41 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵtemplate"](0, Example4Component_div_0_Template, 4, 0, "div", 0); |
| 42 | + } if (rf & 2) { |
| 43 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵproperty"]("ngIf", true); |
| 44 | + } }, directives: [_angular_common__WEBPACK_IMPORTED_MODULE_2__["NgIf"]], styles: ["... */"] }); |
| 45 | +/*@__PURE__*/ (function () { _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵsetClassMetadata"](Example4Component, [{ |
| 46 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["Component"], |
| 47 | + args: [{ |
| 48 | + selector: 'app-example4', |
| 49 | + templateUrl: './example4.component.html', |
| 50 | + styleUrls: ['../example1/example1.component.sass'] |
| 51 | + }] |
| 52 | + }], function () { return []; }, { uc1: [{ |
| 53 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["ViewChild"], |
| 54 | + args: ['uc1', { static: true }] |
| 55 | + }], uc2: [{ |
| 56 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["ViewChild"], |
| 57 | + args: ['uc2', { static: true }] |
| 58 | + }] }); })(); |
| 59 | + |
| 60 | + |
| 61 | +/***/ }), |
| 62 | +``` |
| 63 | + |
| 64 | +```javascript |
| 65 | +// Static is set to False |
| 66 | +const _c0 = ["uc1"]; |
| 67 | +const _c1 = ["uc2"]; |
| 68 | +function Example4Component_div_0_Template(rf, ctx) { if (rf & 1) { |
| 69 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementStart"](0, "div", null, 1); |
| 70 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementStart"](2, "p"); |
| 71 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵtext"](3, "My amazing content !"); |
| 72 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementEnd"](); |
| 73 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelementEnd"](); |
| 74 | +} } |
| 75 | +class Example4Component extends _app_example1_a_component__WEBPACK_IMPORTED_MODULE_1__["AComponent"] { |
| 76 | + constructor() { |
| 77 | + super(); |
| 78 | + // this.printRef.push(() => this.uc1); |
| 79 | + this.printRef.push(() => this.uc2); |
| 80 | + } |
| 81 | +} |
| 82 | +Example4Component.ɵfac = function Example4Component_Factory(t) { return new (t || Example4Component)(); }; |
| 83 | +Example4Component.ɵcmp = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵdefineComponent"]({ type: Example4Component, selectors: [["app-example4"]], viewQuery: function Example4Component_Query(rf, ctx) { if (rf & 1) { |
| 84 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵstaticViewQuery"](_c0, true); |
| 85 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵviewQuery"](_c1, true); |
| 86 | + } if (rf & 2) { |
| 87 | + var _t; |
| 88 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵqueryRefresh"](_t = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵloadQuery"]()) && (ctx.uc1 = _t.first); |
| 89 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵqueryRefresh"](_t = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵloadQuery"]()) && (ctx.uc2 = _t.first); |
| 90 | + } }, features: [_angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵInheritDefinitionFeature"]], decls: 1, vars: 1, consts: [[4, "ngIf"], ["uc2", ""]], template: function Example4Component_Template(rf, ctx) { if (rf & 1) { |
| 91 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵtemplate"](0, Example4Component_div_0_Template, 4, 0, "div", 0); |
| 92 | + } if (rf & 2) { |
| 93 | + _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵproperty"]("ngIf", true); |
| 94 | + } }, directives: [_angular_common__WEBPACK_IMPORTED_MODULE_2__["NgIf"]], styles: ["..."] }); |
| 95 | +/*@__PURE__*/ (function () { _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵsetClassMetadata"](Example4Component, [{ |
| 96 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["Component"], |
| 97 | + args: [{ |
| 98 | + selector: 'app-example4', |
| 99 | + templateUrl: './example4.component.html', |
| 100 | + styleUrls: ['../example1/example1.component.sass'] |
| 101 | + }] |
| 102 | + }], function () { return []; }, { uc1: [{ |
| 103 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["ViewChild"], |
| 104 | + args: ['uc1', { static: true }] |
| 105 | + }], uc2: [{ |
| 106 | + type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["ViewChild"], |
| 107 | + args: ['uc2', { static: false }] |
| 108 | + }] }); })(); |
| 109 | + |
| 110 | + |
| 111 | +/***/ }), |
| 112 | +``` |
| 113 | + |
| 114 | +## ViewChild Read attribute ([link](https://angular.io/api/core/ViewChild)) |
| 115 | + |
| 116 | +The ViewChild Read attribute allow you to read a different token from the queried elements. |
0 commit comments