| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | # TypeScript to JavaScript
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | ## Introduction
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Anything you can do with Angular in _TypeScript_, you can also do | 
					
						
							|  |  |  | in JavaScript. Translating from one language to the other is mostly a | 
					
						
							|  |  |  | matter of changing the way you organize your code and access Angular APIs. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | _TypeScript_ is a popular language option for Angular development. | 
					
						
							|  |  |  | Most code examples on the Internet as well as on this site are written in _TypeScript_. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | This cookbook contains recipes for translating _TypeScript_ | 
					
						
							|  |  |  | code examples to _ES6_ and to _ES5_ so that JavaScript developers | 
					
						
							|  |  |  | can read and write Angular apps in their preferred dialect. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Run and compare the live <live-example name="ts-to-js/ts">TypeScript</live-example> and <live-example name="ts-to-js/js">JavaScript</live-example> | 
					
						
							|  |  |  | code shown in this cookbook. | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ## _TypeScript_ to _ES6_ to _ES5_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | _TypeScript_ | 
					
						
							| 
									
										
										
										
											2017-04-24 20:23:45 +02:00
										 |  |  | <a href="https://www.typescriptlang.org" title='"TypeScript is a typed, superset of JavaScript"'>is a typed superset of _ES6 JavaScript_</a>. | 
					
						
							| 
									
										
										
										
											2017-03-11 13:44:25 +00:00
										 |  |  | _ES6 JavaScript_ is a superset of _ES5 JavaScript_. _ES5_ is the kind of JavaScript that runs natively in all modern browsers. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The transformation of _TypeScript_ code all the way down to _ES5_ code can be seen as "shedding" features. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | The downgrade progression is as follows: | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | * _TypeScript_ to _ES6-with-decorators_. | 
					
						
							|  |  |  | * _ES6-with-decorators_ to _ES6-without-decorators_ ("_plain ES6_"). | 
					
						
							|  |  |  | * _ES6-without-decorators_ to _ES5_. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | When translating from _TypeScript_ to _ES6-with-decorators_, remove | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | [class property access modifiers](http://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) | 
					
						
							|  |  |  | such as `public` and `private`. | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | Remove most of the | 
					
						
							|  |  |  | [type declarations](https://www.typescriptlang.org/docs/handbook/basic-types.html), | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | such as `:string` and `:boolean` | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | but **keep the constructor parameter types, which are used for dependency injection**. | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | From _ES6-with-decorators_ to _plain ES6_, remove all | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) | 
					
						
							|  |  |  | and the remaining types. | 
					
						
							|  |  |  | You must declare properties in the class constructor (`this.title = '...'`) rather than in the body of the class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Finally, from _plain ES6_ to _ES5_, the main missing features are `import` | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | statements and `class` declarations. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | For _plain ES6_ transpilation you can _start_ with a setup similar to the | 
					
						
							|  |  |  | [_TypeScript_ quickstart](https://github.com/angular/quickstart) and adjust the application code accordingly. | 
					
						
							|  |  |  | Transpile with [Babel](https://babeljs.io/) using the `es2015` preset. | 
					
						
							|  |  |  | To use decorators and annotations with Babel, install the | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | [`angular2`](https://github.com/shuhei/babel-plugin-angular2-annotations) preset as well. | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | {@a modularity} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Importing and Exporting
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Importing Angular Code
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In both _TypeScript_ and _ES6_, you import Angular classes, functions, and other members with _ES6_ `import` statements. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | In _ES5_, you access the Angular entities of the [the Angular packages](guide/glossary#scoped-package) | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | through the global `ng` object. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Anything you can import from `@angular` is a nested member of this `ng` object: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/app.module.ts" region="ng2import"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/app.module.es6" region="ng2import"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/app.module.es6" region="ng2import"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/app.module.js" region="ng2import"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | ### Exporting application code
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Each file in a _TypeScript_ or _ES6_ Angular application constitutes an _ES6_ module. | 
					
						
							|  |  |  | When you want to make something available to other modules, you `export` it. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | _ES5_ lacks native support for modules. | 
					
						
							|  |  |  | In an Angular _ES5_ application, you load each file manually by adding a `<script>` tag to `index.html`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="alert is-important"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   The order of `<script>` tags is often significant. | 
					
						
							|  |  |  |   You must load a file that defines a public JavaScript entity before a file that references that entity. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | The best practice in _ES5_ is to create a form of modularity that avoids polluting the global scope. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Add one application namespace object such as `app` to the global `document`. | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Then each code file "exports" public entities by attaching them to that namespace object, for example, `app.HeroComponent`. | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | You could factor a large application into several sub-namespaces | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | which leads to "exports" along the lines of `app.heroQueries.HeroComponent`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | Every _ES5_ file should wrap code in an | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | [Immediately Invoked Function Expression (IIFE)](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) | 
					
						
							|  |  |  | to limit unintentional leaking of private symbols into the global scope. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Here is a `HeroComponent` as it might be defined and "exported" in each of the four language variants. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero.component.ts" region="appexport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero.component.es6" region="appexport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero.component.es6" region="appexport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero.component.js" region="appexport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | ### Importing application Code
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | In _TypeScript_ and _ES6_ apps, you `import` things that have been exported from other modules. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _ES5_ you use the shared namespace object to access "exported" entities from other files. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/app.module.ts" region="appimport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/app.module.es6" region="appimport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/app.module.es6" region="appimport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/app.module.js" region="appimport"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="alert is-helpful"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   Alternatively, you can use a module loader such as Webpack or | 
					
						
							|  |  |  |   Browserify in an Angular JavaScript project. In such a project, you   would | 
					
						
							|  |  |  |   use _CommonJS_ modules and the `require` function to load Angular   framework code. | 
					
						
							|  |  |  |   Then use `module.exports` and `require` to export and import  application  code. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | {@a class-metadata} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Classes and Class Metadata
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Classes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Most Angular _TypeScript_ and _ES6_ code is written as classes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Properties and method parameters of _TypeScript_ classes may be marked with the access modifiers | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | `private`, `internal`, and `public`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Remove these modifiers when translating to JavaScript. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Most type declarations, for example, `:string` and `:boolean`, should be removed when translating to JavaScript. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | When translating to _ES6-with-decorators_, ***do not remove types from constructor parameters!*** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Look for types in _TypeScript_ property declarations. | 
					
						
							|  |  |  | In general it is better to initialize such properties with default values because | 
					
						
							|  |  |  | many browser JavaScript engines can generate more performant code. | 
					
						
							|  |  |  | When _TypeScript_ code follows this same advice, it can infer the property types | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | and there is nothing to remove during translation. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | In _ES6-without-decorators_, properties of classes must be assigned inside the constructor. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | _ES5_ JavaScript has no classes. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Use the constructor function pattern instead, adding methods to the prototype. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero.component.ts" region="class"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero.component.es6" region="class"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero.component.es6" region="class"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero.component.js" region="constructorproto"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Metadata
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | When writing in _TypeScript_ or _ES6-with-decorators_, | 
					
						
							|  |  |  | provide configuration and metadata by adorning a class with one or more *decorators*. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | For example, you supply metadata to a component class by preceding its definition with a | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | [`@Component`](api/core/Component) decorator function whose | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | argument is an object literal with metadata properties. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _plain ES6_, you provide metadata by attaching an `annotations` array to the _class_. | 
					
						
							|  |  |  | Each item in the array is a new instance of a metadata decorator created with a similar metadata object literal. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _ES5_, you also provide an `annotations` array but you attach it to the _constructor function_ rather than to a class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | See these variations side-by-side: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero.component.ts" region="metadata"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero.component.es6" region="metadata"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero.component.es6" region="metadata"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero.component.js" region="metadata"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | ## External template file
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | A large component template is often kept in a separate template file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  | <code-example path="ts-to-js/ts/src/app/hero-title.component.html" title="src/app/hero-title.component.html" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | The component, `HeroTitleComponent` in this case, then references the template file in its metadata `templateUrl` property: | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-title.component.ts" region="templateUrl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-title.component.es6" region="templateUrl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-title.component.es6" region="templateUrl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-title.component.js" region="templateUrl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that both the _TypeScript_ and _ES6_ `templateUrl` properties identify the location of the template file _relative to the component module_. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | {@a dsl} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## _ES5_ DSL
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | This _ES5_ pattern of creating a constructor and annotating it with metadata is so common that Angular | 
					
						
							|  |  |  | provides a convenience API to make it a little more compact and locates the metadata above the constructor, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | as you would if you wrote in _TypeScript_ or _ES6-with-decorators_. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This _API_ (_Application Programming Interface_) is commonly known as the _ES5 DSL_ (_Domain Specific Language_). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Set an application namespace property, for example, `app.HeroDslComponent`, to the result of an `ng.core.Component` function call. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Pass the same metadata object to `ng.core.Component` as you did before. | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Then chain a call to the `Class()` method which takes an object defining the class constructor and instance methods. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Here is an example of the `HeroComponent`, re-written with the DSL, | 
					
						
							|  |  |  | next to the original _ES5_ version for comparison: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 22:20:43 -07:00
										 |  |  | {@a name-constructor} | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 22:20:43 -07:00
										 |  |  | <div class="callout is-helpful"> | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   A **named** constructor displays clearly in the console log | 
					
						
							|  |  |  |   if the component throws a runtime error. | 
					
						
							|  |  |  |   An **unnamed** constructor displays as an anonymous function, for   example, `class0`, | 
					
						
							|  |  |  |   which is impossible to find in the source code. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | {@a getters-setters} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ### Properties with getters and setters
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _TypeScript_ and _ES6_ support with getters and setters. | 
					
						
							|  |  |  | Here's an example of a read-only _TypeScript_ property with a getter | 
					
						
							|  |  |  | that prepares a toggle-button label for the next clicked state: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  | <code-example path="ts-to-js/ts/src/app/hero-queries.component.ts" region="defined-property" title="ts/src/app/hero-queries.component.ts" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | This _TypeScript_ "getter" property is transpiled to an _ES5_ | 
					
						
							|  |  |  | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" | 
					
						
							| 
									
										
										
										
											2017-04-24 20:23:45 +02:00
										 |  |  |    title="Defined Properties">defined property</a>. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The _ES5 DSL_ does not support _defined properties_ directly | 
					
						
							|  |  |  | but you can still create them by extracting the "class" prototype and | 
					
						
							|  |  |  | adding the _defined property_ in raw JavaScript like this: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  | <code-example path="ts-to-js/js/src/app/hero-queries.component.js" region="defined-property" title="js/src/app/hero-queries.component.ts" linenums="false"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | </code-example> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | {@a dsl-other} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | ### DSL for other classes
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | There are similar DSLs for other decorated classes. | 
					
						
							|  |  |  | You can define a directive with `ng.core.Directive`: | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | <code-example> | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  |   app.MyDirective = ng.core.Directive({ | 
					
						
							|  |  |  |     selector: '[myDirective]' | 
					
						
							|  |  |  |   }).Class({ | 
					
						
							|  |  |  |     ... | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | </code-example> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | and a pipe with `ng.core.Pipe`: | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | <code-example> | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  |   app.MyPipe = ng.core.Pipe({ | 
					
						
							|  |  |  |     name: 'myPipe' | 
					
						
							|  |  |  |   }).Class({ | 
					
						
							|  |  |  |     ... | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | </code-example> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {@a interfaces} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Interfaces
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A _TypeScript_ interface helps ensure that a class implements the interface's members correctly. | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Always try to use Angular interfaces where appropriate. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | For example, the component class that implements the `ngOnInit` lifecycle hook method | 
					
						
							|  |  |  | should implement the `OnInit` interface. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _TypeScript_ interfaces exist for developer convenience and are not used by Angular at runtime. | 
					
						
							|  |  |  | They have no physical manifestation in the generated JavaScript code. | 
					
						
							|  |  |  | Just implement the methods and ignore interfaces when translating code samples from _TypeScript_ to JavaScript. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-lifecycle.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-lifecycle.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-lifecycle.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-lifecycle.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-lifecycle.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | {@a io-decorators} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Input and Output Metadata
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Input and Output Decorators
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _TypeScript_ and _ES6-with-decorators_, you often add metadata to class _properties_ with _property decorators_. | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | For example, you apply [`@Input` and `@Output` property decorators](guide/template-syntax#inputs-outputs) | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | to public class properties that will be the target of data binding expressions in parent components. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | There is no equivalent of a property decorator in _ES5_ or _plain ES6_. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Fortunately, every property decorator has an equivalent representation in a class decorator metadata property. | 
					
						
							|  |  |  | A _TypeScript_ `@Input` property decorator can be represented by an item in the `Component` metadata's `inputs` array. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You already know how to add `Component` or `Directive` class metadata in _any_ JavaScript dialect so | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | there's nothing fundamentally new about adding another property. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | But note that what would have been _separate_ `@Input` and `@Output` property decorators for each class property are | 
					
						
							|  |  |  | combined in the metadata `inputs` and `outputs` _arrays_. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/confirm.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/confirm.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/confirm.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/confirm.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/confirm.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | In the previous example, one of the public-facing binding names, `cancelMsg`, | 
					
						
							|  |  |  | differs from the corresponding class property name, `notOkMsg`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | That's OK but you must tell Angular about it so that it can map an external binding of `cancelMsg` | 
					
						
							|  |  |  | to the component's `notOkMsg` property. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | In _TypeScript_ and _ES6-with-decorators_, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | you specify the special binding name in the argument to the property decorator. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _ES5_ and _plain ES6_ code, convey this pairing with the `propertyName: bindingName` syntax in the class metadata. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | {@a dependency-injection} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | ## Dependency injection
 | 
					
						
							| 
									
										
										
										
											2017-03-11 15:36:40 +00:00
										 |  |  | Angular relies heavily on [Dependency Injection](guide/dependency-injection) to provide services to the objects it creates. | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | When Angular creates a new component, directive, pipe or another service, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | it sets the class constructor parameters to instances of services provided by an _Injector_. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The developer must tell Angular what to inject into each parameter. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | {@a injection-class-type} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Injection by class type
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The easiest and most popular technique in _TypeScript_ and _ES6-with-decorators_ is to set the constructor parameter type | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | to the class associated with the service to inject. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The _TypeScript_ transpiler writes parameter type information into the generated JavaScript. | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Angular reads that information at runtime and locates the corresponding service in the appropriate _Injector_. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The _ES6-with-decorators_ transpiler does essentially the same thing using the same parameter-typing syntax. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | _ES5_ and _plain ES6_ lack types so you must identify "injectables" by attaching a **`parameters`** array to the constructor function. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Each item in the array specifies the service's injection token. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | As with _TypeScript_, the most popular token is a class, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | or rather a _constructor function_ that represents a class in _ES5_ and _plain ES6_. | 
					
						
							|  |  |  | The format of the `parameters` array varies: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | * _Plain ES6_—nest each constructor function in a sub-array. | 
					
						
							|  |  |  | * _ES5_—simply list the constructor functions. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | When writing with _ES5 DSL_, set the `Class.constructor` property to | 
					
						
							|  |  |  | an array whose first parameters are the injectable constructor functions and whose | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | last parameter is the class constructor itself. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | This format should be familiar to AngularJS developers. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-di.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-di.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-di.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-di.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-di.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Injection with the @Inject decorator
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sometimes the dependency injection token isn't a class or constructor function. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _TypeScript_ and _ES6-with-decorators_, you precede the class constructor parameter | 
					
						
							|  |  |  | by calling the `@Inject()` decorator with the injection token. | 
					
						
							|  |  |  | In the following example, the token is the string `'heroName'`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  | The other JavaScript dialects add a `parameters` array to the class constructor function. | 
					
						
							| 
									
										
										
										
											2017-07-07 16:55:17 -07:00
										 |  |  | Each item contains a new instance of `Inject`: | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | * _Plain ES6_—each item is a new instance of `Inject(token)` in a sub-array. | 
					
						
							|  |  |  | * _ES5_—simply list the string tokens. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | When writing with _ES5 DSL_, set the `Class.constructor` property to a function definition | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | array as before. Create a new instance of `ng.core.Inject(token)` for each parameter. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-di-inject.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-di-inject.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-di-inject.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-di-inject.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-di-inject.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Additional Injection Decorators
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You can qualify injection behavior with injection decorators from `@angular/core`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | In _TypeScript_ and _ES6-with-decorators_, | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | you precede the constructor parameters with injection qualifiers such as: | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | * [`@Optional`](api/core/Optional) sets the parameter to `null` if the service is missing. | 
					
						
							|  |  |  | * [`@Attribute`](api/core/Attribute) to inject a host element attribute value. | 
					
						
							|  |  |  | * [`@ContentChild`](api/core/ContentChild) to inject a content child. | 
					
						
							|  |  |  | * [`@ViewChild`](api/core/ViewChild) to inject a view child. | 
					
						
							|  |  |  | * [`@Host`](api/core/Host) to inject a service in this component or its host. | 
					
						
							|  |  |  | * [`@SkipSelf`](api/core/SkipSelf) to inject a service provided in an ancestor of this component. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | In _plain ES6_ and _ES5_, create an instance of the equivalent injection qualifier in a nested array within the `parameters` array. | 
					
						
							|  |  |  | For example, you'd write `new Optional()` in _plain ES6_ and `new ng.core.Optional()` in _ES5_. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When writing with _ES5 DSL_, set the `Class.constructor` property to a function definition | 
					
						
							|  |  |  | array as before. Use a nested array to define a parameter's complete injection specification. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-title.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-title.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-title.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-title.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-title.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="l-sub-section"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   In the example above, there is no provider for the `'titlePrefix'` token. | 
					
						
							|  |  |  |   Without `@Optional()`, Angular would raise an error. | 
					
						
							|  |  |  |   With `@Optional()`, Angular sets the constructor parameter to `null` | 
					
						
							|  |  |  |   and the component displays the title without a prefix. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | {@a host-binding} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Host Binding
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Angular supports bindings to properties and events of the _host element_, which is the | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | element whose tag matches the component selector. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Host Decorators
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _TypeScript_ and _ES6-with-decorators_, you can use host property decorators to bind a host | 
					
						
							|  |  |  | element to a component or directive. | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | The [`@HostBinding`](api/core/HostBinding) decorator | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | binds host element properties to component data properties. | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | The [`@HostListener`](api/core/HostListener) decorator binds | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | host element events to component event handlers. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In _plain ES6_ or _ES5_, add a `host` attribute to the component metadata to achieve the | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | same effect as `@HostBinding` and `@HostListener`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The  `host` value is an object whose properties are host property and listener bindings: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * Each key follows regular Angular binding syntax: `[property]` for host bindings | 
					
						
							|  |  |  |   or `(event)` for host listeners. | 
					
						
							|  |  |  | * Each value identifies the corresponding component property or method. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-host.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-host.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-host.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript" path="ts-to-js/js/src/app/hero-host.component.js"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-host.component.js" region="dsl"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Host Metadata
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Some developers prefer to specify host properties and listeners | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | in the component metadata. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | They'd _rather_ do it the way you _must_ do it _ES5_ and _plain ES6_. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | The following re-implementation of the `HeroComponent` shows that _any property metadata decorator_ | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | can be expressed as component or directive metadata in both _TypeScript_ and _ES6-with-decorators_. | 
					
						
							|  |  |  | These particular _TypeScript_ and _ES6_ code snippets happen to be identical. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-host-meta.component.ts"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-host-meta.component.es6"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | {@a view-child-decorators} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### View and Child Decorators
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Several _property_ decorators query a component's nested view and content components. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="l-sub-section"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   _View_ children are associated with element tags that appear _within_ the component's template. | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   _Content_ children are associated with elements that appear _between_ the component's element tags; | 
					
						
							|  |  |  |   they are projected into an `<ng-content>` slot in the component's template. | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | The [`@ViewChild`](api/core/ViewChild) and | 
					
						
							|  |  |  | [`@ViewChildren`](api/core/ViewChildren) property decorators | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | allow a component to query instances of other components that are used in | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | its view. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:23:16 +01:00
										 |  |  | In _ES5_ and _ES6_, you access a component's view children by adding a `queries` property to the component metadata. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The `queries` property value is a hash map. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | * Each _key_ is the name of a component property that will hold the view child or children. | 
					
						
							|  |  |  | * Each _value_ is a new instance of either `ViewChild` or `ViewChildren`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-queries.component.ts" region="view"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-queries.component.es6" region="view"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-queries.component.es6" region="view"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-queries.component.js" region="view"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | The [`@ContentChild`](api/core/ContentChild) and | 
					
						
							|  |  |  | [`@ContentChildren`](api/core/ContentChildren) property decorators | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | allow a component to query instances of other components that have been projected | 
					
						
							|  |  |  | into its view from elsewhere. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | They can be added in the same way as [`@ViewChild`](api/core/ViewChild) and | 
					
						
							|  |  |  | [`@ViewChildren`](api/core/ViewChildren). | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="TypeScript" path="ts-to-js/ts/src/app/hero-queries.component.ts" region="content"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-06-07 10:59:48 -07:00
										 |  |  |   <code-pane title="ES6 + Decorators" path="ts-to-js/js-es6-decorators/src/app/hero-queries.component.es6" region="content"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES6 JavaScript" path="ts-to-js/js-es6/src/app/hero-queries.component.es6" region="content"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  |   <code-pane title="ES5 JavaScript with DSL" path="ts-to-js/js/src/app/hero-queries.component.js" region="content"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  |   </code-pane> | 
					
						
							|  |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | <div class="alert is-helpful"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  |   In _TypeScript_ and _ES6-with-decorators_ you can also use the  `queries` metadata | 
					
						
							|  |  |  |   instead of the `@ViewChild` and `@ContentChild` property decorators. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | {@a aot} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## AOT Compilation in _TypeScript_ only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 18:34:00 -04:00
										 |  |  | Angular offers two modes of template compilation, JIT (_just-in-time_) and | 
					
						
							|  |  |  | [AOT (_ahead-of-time_)](guide/aot-compiler). | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Currently the AOT compiler only works with _TypeScript_ applications because, in part, it generates | 
					
						
							|  |  |  | _TypeScript_ files as an intermediate result. | 
					
						
							| 
									
										
										
										
											2017-04-20 13:33:31 +02:00
										 |  |  | **AOT is not an option for pure JavaScript applications** at this time. |