You can compile Angular applications at build time.
By compiling your application using the compiler-cli, ngc, you can bootstrap directly
to a module factory, meaning you don't need to include the Angular compiler in your JavaScript bundle.
Ahead-of-time compiled applications also benefit from decreased load time and increased performance.
Helps you organize an application into cohesive blocks of functionality.
An Angular module identifies the components, directives, and pipes that the application uses along with the list of external Angular modules that the application needs, such as FormsModule.
Every Angular application has an application root-module class. By convention, the class is
called AppModule and resides in a file named app.module.ts.
指令 (directive)的一种。可以监听或修改其它 HTML 元素、特性 (attribute)、属性 (property)、组件的行为。通常用作 HTML 属性,就像它的名字所暗示的那样。
A category of directive that can listen to and modify the behavior of
other HTML elements, attributes, properties, and components. They are usually represented
as HTML attributes, hence the name.
例如,ngClass指令就是典型的属性型指令。它可以添加或移除 CSS 类名。
For example, you can use the ngClass directive to add and remove CSS class names.
A way to rollup exports from several ES2015 modules into a single convenient ES2015 module.
The barrel itself is an ES2015 module file that re-exports selected exports of other ES2015 modules.
例如,设想在heroes目录下有三个 ES2015 模块:
For example, imagine three ES2015 modules in a heroes folder:
You can add a barrel to the heroes folder (called index, by convention) that exports all of these items:
export*from'./hero.model.ts';// re-export all of its exportsexport*from'./hero.service.ts';// re-export all of its exportsexport{HeroComponent}from'./hero.component.ts';// re-export the named thing
现在,消费者就就可以从这个封装桶中导入它需要的东西了。
Now a consumer can import what it needs from the barrel.
import{Hero,HeroService}from'../heroes';// index is implied
You launch an Angular application by "bootstrapping" it using the application root Angular module (AppModule).
Bootstrapping identifies an application's top level "root" component,
which is the first component that is loaded for the application.
For more information, see the Setup page.
你可以在同一个index.html中引导多个应用,每个应用都有它自己的顶级根组件。
You can bootstrap multiple apps in the same index.html, each app with its own top-level root.
驼峰式命名法 (camelCase)
camelCase
驼峰式命名法是书写复合词或短语的一种形式,除首字母要小写外,每个单词或缩写都以大写字母开头。
The practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter
except the first letter, which is lowercase.
Function, property, and method names are typically spelled in camelCase. For example, square, firstName, and getHeroes. Notice that square is an example of how you write a single word in camelCase.
camelCase is also known as lower camel case to distinguish it from upper camel case, or PascalCase.
In Angular documentation, "camelCase" always means lower camel case.
Apply the @Componentdecorator to
the component class, thereby attaching to the class the essential component metadata
that Angular needs to create a component instance and render the component with its template
as a view.
Directive selectors (like my-app) and
the root of filenames (such as hero-list.component.ts) are often
spelled in dash-case.
数据绑定 (data binding)
Data binding
应用程序会将数据展示给用户,并对用户的操作(点击、触屏、按键)做出回应。
Applications display data values to a user and respond to user
actions (such as clicks, touches, and keystrokes).
在数据绑定机制下,我们只要声明一下HTML部件和数据源之间的关系,把细节交给框架去处理。
而以前的手动操作过程是:将数据推送到 HTML 页面中、添加事件监听器、从屏幕获取变化后的数据,并更新应用中的值。
In data binding, you declare the relationship between an HTML widget and data source
and let the framework handle the details.
Data binding is an alternative to manually pushing application data values into HTML, attaching
event listeners, pulling changed values from the screen, and
updating application data values.
Angular 有一个非常强大的数据绑定框架,具有各种数据绑定操作,并支持声明式语法。
Angular has a rich data-binding framework with a variety of data-binding
operations and supporting declaration syntax.
Angular has its own set of decorators to help it interoperate with your application parts.
The following example is a @Component decorator that identifies a
class as an Angular component and an @Input decorator applied to the name property
of that component. The elided object argument to the @Component decorator would contain the pertinent component metadata.
The scope of a decorator is limited to the language feature
that it decorates. None of the decorations shown here will "leak" to other
classes that follow it in the file.
永远别忘了在装饰器后面加括号()。
Always include parentheses () when applying a decorator.
These parts often rely on other parts. An Angular component
part might rely on a service part to get data or perform a calculation. When
part "A" relies on another part "B," you say that "A" depends on "B" and
that "B" is a dependency of "A."
You can ask a "dependency injection system" to create "A"
for us and handle all the dependencies.
If "A" needs "B" and "B" needs "C," the system resolves that chain of dependencies
and returns a fully prepared instance of "A."
Angular provides and relies upon its own sophisticated
dependency-injection system
to assemble and run applications by "injecting" application parts
into other application parts where and when needed.
A token is an Angular type (InjectionToken). You rarely need to work with tokens directly; most
methods accept a class name (Foo) or a string ("foo") and Angular converts it
to a token. When you write injector.get(Foo), the injector returns
the value associated with the token for the Foo class, typically an instance of Foo itself.
The Injector maintains an internal map of tokens to dependency values.
If the Injector can't find a value for a given token, it creates
a new value using a Provider for that token.
An injector can only create a value for a given token if it has
a provider for that token in its internal provider registry.
Registering providers is a critical preparatory step.
Angular 会为每个注册器注册很多内置提供商。
我们也可以注册自己的提供商。
Angular registers some of its own providers with every injector.
You can register your own providers.
指令是一个 Angular 类,负责创建和重塑浏览器 DOM 中的 HTML 元素,并与之互动。
指令是 Angular 中最基本的特性之一。
An Angular class responsible for creating, reshaping, and interacting with HTML elements
in the browser DOM. The directive is Angular's most fundamental feature.
指令几乎总与 HTML 元素或属性 (attribute) 相关。
我们通常把这些关联到的 HTML 元素或者属性 (attribute) 当做指令本身。
当 Angular 在 HTML 模板中遇到一个指令的时候,
它会创建匹配的指令类的实例,并把浏览器中这部分 DOM 的控制权交给它。
A directive is usually associated with an HTML element or attribute.
This element or attribute is often referred to as the directive itself.
When Angular finds a directive in an HTML template,
it creates the matching directive class instance
and gives the instance control over that portion of the browser DOM.
你可以自定义 HTML 标签(例如<my-directive>)来关联自定义指令。
然后,可以像写原生 HTML 一样把这些自定义标签放到 HTML 模板里。
这样,指令就变成了 HTML 本身的拓展。
You can invent custom HTML markup (for example, <my-directive>) to
associate with your custom directives. You add this custom markup to HTML templates
as if you were writing native HTML. In this way, directives become extensions of
HTML itself.
指令分为三类:
Directives fall into one of the following categories:
组件 (component): 用于组合程序逻辑和 HTML 模板,渲染出应用程序的视图。
组件一般表示成 HTML 元素的形式,它们是构建 Angular 应用程序的基本单元。
Components combine application logic with an HTML template to
render application views. Components are usually represented as HTML elements.
They are the building blocks of an Angular application.
属性型指令 (attribute directive):可以监控和修改其它 HTML 元素、
HTML 属性 (attribute)、 DOM 属性 (property)、组件等行为等等。它们通常表示为 HTML 属性 (attibute),故名。
Attribute directives can listen to and modify the behavior of
other HTML elements, attributes, properties, and components. They are usually represented
as HTML attributes, hence the name.
Structural directives are responsible for
shaping or reshaping HTML layout, typically by adding, removing, or manipulating
elements and their children.
The latest approved version of JavaScript is
ECMAScript 2016
(also known as "ES2016" or "ES7"). Many Angular developers write their applications
in ES7 or a dialect that strives to be
compatible with it, such as TypeScript.
Most modern browsers only support the much older "ECMAScript 5" (also known as "ES5") standard.
Applications written in ES2016, ES2015, or one of their dialects must be transpiled
to ES5 JavaScript.
A directive property that can be the target of a
property binding (explained in detail in the Template Syntax page).
Data values flow into this property from the data source identified
in the template expression to the right of the equal sign.
A form of property data binding in which a
template expression between double-curly braces
renders as text. That text may be concatenated with neighboring text
before it is assigned to an element property
or displayed between element tags, as in this example.
A bootstrapping method of compiling components and modules in the browser
and launching the application dynamically. Just-in-time mode is a good choice during development.
Consider using the ahead-of-time mode for production apps.
Each interface has a single hook method whose name is the interface name prefixed with ng.
For example, the OnInit interface has a hook method named ngOnInit.
Angular 会按以下顺序调用钩子方法:
Angular calls these hook methods in the following order:
An application that adheres to this standard requires a module loader to
load modules on request and resolve inter-module dependencies.
Angular doesn't include a module loader and doesn't have a preference
for any particular third- party library (although most examples use SystemJS).
You can use any module library that conforms to the standard.
Modules are typically named after the file in which the exported thing is defined.
The Angular DatePipe
class belongs to a feature module named date_pipe in the file date_pipe.ts.
An array whose items arrive asynchronously over time.
Observables help you manage asynchronous data, such as data coming from a backend service.
Observables are used within Angular itself, including Angular's event system and its HTTP client service.
To use observables, Angular uses a third-party library called Reactive Extensions (RxJS).
Observables are a proposed feature for ES 2016, the next version of JavaScript.
A directive property that can be the target of event binding
(read more in the event binding
section of the Template Syntax page).
Events stream out of this property to the receiver identified
in the template expression to the right of the equal sign.
The practice of writing individual words, compound words, or phrases such that each word or abbreviation begins with a capital letter.
Class names are typically spelled in PascalCase. For example, Person and HeroDetailComponent.
This form is also known as upper camel case to distinguish it from lower camel case or simply camelCase.
In this documentation, "PascalCase" means upper camel case and "camelCase" means lower camel case.
An Angular pipe is a function that transforms input values to output values for
display in a view.
Here's an example that uses the built-in currency pipe to display
a numeric value in the local currency.
A provider creates a new instance of a dependency for the
dependency injection system.
It relates a lookup token to code—sometimes called a "recipe"—that can create a dependency value.
Most applications consist of many screens or views.
The user navigates among them by clicking links and buttons,
and performing other similar actions that cause the application to
replace one view with another.
Angular 的组件路由器是一个特性丰富的机制,可以配置和管理整个导航过程,包括建立和销毁视图。
The Angular component router is a richly featured mechanism for configuring and managing the entire view navigation process, including the creation and destruction
of views.
Angular modules are delivered within scoped packages such as @angular/core,
@angular/common, @angular/platform-browser-dynamic, @angular/http, and @angular/router.
Import a scoped package the same way that you import a normal package.
The only difference, from a consumer perspective,
is that the scoped package name begins with the Angular scope name, @angular.
import{Component}from'@angular/core';
服务 (service)
Service
服务用于封装不与任何特定视图相关的数据和逻辑,或者用于在组件之间共享数据和逻辑。
For data or logic that is not associated
with a specific view or that you want to share across components, build services.
应用程序经常需要服务,例如英雄数据服务或者日志服务。
Applications often require services such as a hero data service or a logging service.
A service is a class with a focused purpose.
You often create a service to implement features that are
independent from any specific view,
provide shared data or logic across components, or encapsulate external interactions.
应用通常都需要服务,比如数据服务或者日志服务。
Applications often require services such as a data service or a logging service.
The practice of writing compound words or phrases such that an
underscore (_) separates one word from the next. This form is also known as underscore case.
Structural directives
结构型指令是指令 (directive)一种,
可以通过在DOM中添加、删除或操作元素和其各级子元素来塑造或重塑 HTML 布局。
例如,ngIf这个“条件化元素”指令,ngFor这个“重复器”指令都是众所周知的例子。
A category of directive that can
shape or reshape HTML layout, typically by adding and removing elements in the DOM.
The ngIf "conditional element" directive and the ngFor "repeater" directive are well-known examples.
TypeScript 还以它的可选类型系统而著称。
该类型系统提供了编译时类型检查和强大的工具支持(例如 “Intellisense”,代码补齐,重构和智能搜索等)。
许多代码编辑器和 IDE 都原生支持 TypeScript 或通过插件提供支持。
TypeScript is also notable for its optional typing system, which provides
compile-time type checking and strong tooling support (such as "intellisense,"
code completion, refactoring, and intelligent search). Many code editors
and IDEs support TypeScript either natively or with plugins.
Angular renders a view under the control of one or more directives,
especially component directives and their companion templates.
The component plays such a prominent role that it's often
convenient to refer to a component as a view.
Views often contain other views. Any view might be loaded and unloaded
dynamically as the user navigates through the application, typically
under the control of a router.
区域 (zone)
Zone
区域是一种用来封装和截听 JavaScript 应用程序异步活动的机制。
A mechanism for encapsulating and intercepting
a JavaScript application's asynchronous activity.
浏览器中的 DOM 和 JavaScript 之间常会有一些数量有限的异步活动,
例如 DOM 事件(例如点击)、承诺 (promise)
和通过 XHR 调用远程服务。
The browser DOM and JavaScript have a limited number
of asynchronous activities, such as DOM events (for example, clicks),
promises, and
XHR
calls to remote servers.
区域能截听所有这些活动,并让“区域的客户”有机会在异步活动完成之前和之后采取行动。
Zones intercept all of these activities and give a "zone client" the opportunity
to take action before and after the async activity finishes.
Angular 会在一个 Zone 区域中运行应用程序,在这个区域中,它可以对异步事件做出反应,可以通过检查数据变更、利用数据绑定 (data bindings) 来更新信息显示。
Angular runs your application in a zone where it can respond to
asynchronous events by checking for data changes and updating
the information it displays via data bindings.