include ../../../../_includes/_util-fns
:marked
Everything that we can do in Angular in TypeScript, we can also do
in JavaScript. Translating from one language to the other is mostly a
matter of changing the way we organize our code and the way we access
Angular APIs.
在Angular中,所有能用TypeScript完成的事,也都能用JavaScript完成。
从一个语言换成另一个语言,最多只会影响源代码的组织方式和访问Angular API的方法。
Since TypeScript is a popular language option in Angular, many of the
code examples you see on the Internet as well as on this site are written
in TypeScript. This cookbook contains recipes for translating these kinds of
code examples to ES5, so that they can be applied to Angular JavaScript
applications.
TypeScript是个广受欢迎的Angular 2语言选项,你在网络上和本站上看到的很多范例代码都是用TypeScript写的。
本烹饪宝典包含如何把这些代码编译到ES5的方法,这样它们就可以被用到JavaScript版的Angular 2程序里了。
:marked
## Table of contents
## 目录
[Modularity: imports and exports](#modularity)
[模块化:导入与导出](#modularity)
[Classes and Class Metadata](#class-metadata)
[类和“类的元数据”](#class-metadata)
[Input and Output Metadata](#property-metadata)
[`Input`(输入)与`Output`(输出)元数据](#property-metadata)
[Dependency Injection](#dependency-injection)
[依赖注入](#dependency-injection)
[Host and Query Metadata](#other-property-metadata)
[`Host`(宿主)与`Query`(查询)元数据](#other-property-metadata)
**Run and compare the live TypeScript and JavaScript
code shown in this cookbook.**
**运行并比较本烹饪宝典里的在线TypeScript和JavaScript代码**
a(id="modularity")
.l-main-section
:marked
## Importing and Exporting
## 导入与导出
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th TypeScript
th ES5 JavaScript
tr(style=top)
td
:marked
### Importing Angular Code
### 导入Angular代码
In TypeScript code, Angular classes, functions, and other members
are imported with TypeScript `import` statements:
在TypeScript代码中,Angular是利用TypeScript的`import`语句来导入类、函数和其它成员的。
+makeExample('cb-ts-to-js/ts/app/main.ts', 'ng2import')(format="." )
td
:marked
### Accessing Angular Code through the ng global
### 通过全局变量`ng`来访问Angular代码
In JavaScript code, when using
[the Angular packages](../glossary.html#!#scoped-package),
we can access Angular code through the global `ng` object. In the
nested members of this object we'll find everything we would import
from `angular` in TypeScript:
在JavaScript中,当使用[Angular库](../glossary.html#!#scoped-package)时,
我们可以通过全局的`ng`对象来访问Angular代码。在本对象内嵌的很多成员中,我们会发现TypeScript能从`@angular`库导入的所有对应物。
+makeExample('cb-ts-to-js/js/app/main.js', 'ng2import')(format="." )
tr(style=top)
td
:marked
### Importing and Exporting Application Code
### 导入与导出应用程序代码
Each file in an Angular TypeScript application constitutes a
TypeScript module. When we want to make something from a module available
to other modules, we `export` it.
在TypeScript版的Angular 程序里,每个文件都是一个TypeScript模块。当需要让一个模块在其它模块中可见时,我们要`export`它。
+makeExample('cb-ts-to-js/ts/app/hero.component.ts', 'appexport')(format="." )
:marked
In other modules we can then `import` things that have been exported
elsewhere.
然后,我们就可以在其它模块中`import`这些在别处导出的东西。
+makeExample('cb-ts-to-js/ts/app/main.ts', 'appimport')(format="." )
td
:marked
### Sharing Application Code
### 共享应用程序代码
In an Angular JavaScript application, we load each file to the page
using a `