fix: 合并了最新版本
This commit is contained in:
parent
ac300b3db1
commit
6c3d57aee9
@ -333,7 +333,7 @@ JavaScript with [JsDoc](http://usejsdoc.org/) comments needed by the
|
||||
### *annotationsAs*
|
||||
|
||||
Use this option to modify how the Angular specific annotations are emitted to improve tree-shaking. Non-Angular
|
||||
annotations and decorators are unnaffected. Default is `static fields`.
|
||||
annotations and decorators are unaffected. Default is `static fields`.
|
||||
|
||||
使用这个选项来修改生成 Angular 特有注解的方式,以提升摇树优化(tree-shaking)的效果。它对 Angular 自身之外的注解和装饰器无效。
|
||||
默认值是 `static fields`。
|
||||
@ -349,16 +349,6 @@ This tells the compiler to print extra information while compiling templates.
|
||||
|
||||
它告诉编译器在编译模板时打印额外的信息。
|
||||
|
||||
### *enableLegacyTemplate*
|
||||
|
||||
The use of `<template>` element was deprecated starting in Angular 4.0 in favor of using
|
||||
`<ng-template>` to avoid colliding with the DOM's element of the same name. Setting this option to
|
||||
`true` enables the use of the deprecated `<template>` element . This option
|
||||
is `false` by default. This option might be required by some third-party Angular libraries.
|
||||
|
||||
从 Angular 4.0 开始,`<template>` 元素已经被废弃了,要改用 `<ng-template>` 以避免和同名 DOM 元素的冲突。把该选项设置为 `true` 将会允许使用已废弃的 `<template>` 元素。
|
||||
该选项默认为 `false`。某些第三方 Angular 库可能会需要该选项。
|
||||
|
||||
### *disableExpressionLowering*
|
||||
|
||||
The Angular template compiler transforms code that is used, or could be used, in an annotation
|
||||
@ -444,7 +434,7 @@ export class TypicalComponent {
|
||||
|
||||
```
|
||||
|
||||
The Anglar compiler extracts the metadata _once_ and generates a _factory_ for `TypicalComponent`.
|
||||
The Angular compiler extracts the metadata _once_ and generates a _factory_ for `TypicalComponent`.
|
||||
When it needs to create a `TypicalComponent` instance, Angular calls the factory, which produces a new visual element, bound to a new instance of the component class with its injected dependency.
|
||||
|
||||
Angular 编译器只提取**一次**元数据,并且为 `TypicalComponent` 生成一个**工厂**。
|
||||
|
@ -5,9 +5,7 @@
|
||||
</div>
|
||||
|
||||
<header class="api-header">
|
||||
|
||||
<h1><label class="api-status-label experimental">experimental</label><label class="api-type-label class">class</label>Class Name</h1>
|
||||
|
||||
</header>
|
||||
|
||||
<div class="page-actions">
|
||||
|
212
aio/content/guide/architecture-components.md
Normal file
212
aio/content/guide/architecture-components.md
Normal file
@ -0,0 +1,212 @@
|
||||
# Introduction to components
|
||||
|
||||
<img src="generated/images/guide/architecture/hero-component.png" alt="Component" class="left">
|
||||
|
||||
A _component_ controls a patch of screen called a *view*. For example, individual components define and control each of the following views from the [Tutorial](tutorial/index):
|
||||
|
||||
* The app root with the navigation links.
|
||||
|
||||
带有导航链接的应用根组件。
|
||||
|
||||
* The list of heroes.
|
||||
|
||||
英雄列表。
|
||||
|
||||
* The hero editor.
|
||||
|
||||
英雄编辑器。
|
||||
|
||||
You define a component's application logic—what it does to support the view—inside a class. The class interacts with the view through an API of properties and methods.
|
||||
|
||||
你在类中定义组件的应用逻辑,为视图提供支持。
|
||||
组件通过一些由属性和方法组成的 API 与视图交互。
|
||||
|
||||
For example, the `HeroListComponent` has a `heroes` property that returns an array of heroes that it acquires from a service. `HeroListComponent` also has a `selectHero()` method that sets a `selectedHero` property when the user clicks to choose a hero from that list.
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (class)" region="class"></code-example>
|
||||
|
||||
Angular creates, updates, and destroys components as the user moves through the application. Your app can take action at each moment in this lifecycle through optional [lifecycle hooks](guide/lifecycle-hooks), like `ngOnInit()`.
|
||||
|
||||
<hr/>
|
||||
|
||||
## Component metadata
|
||||
|
||||
<img src="generated/images/guide/architecture/metadata.png" alt="Metadata" class="left">
|
||||
|
||||
The `@Component` decorator identifies the class immediately below it as a component class, and specifies its metadata. In the example code below, you can see that `HeroListComponent` is just a class, with no special Angular notation or syntax at all. It's not a component until mark it as one with the `@Component` decorator.
|
||||
|
||||
The metadata for a component tells Angular where to get the major building blocks it needs to create and present the component and its view. In particular, it associates a _template_ with the component, either directly with inline code, or by reference. Together, the component and its template describe a _view_.
|
||||
|
||||
In addition to containing or pointing to the template, the `@Component` metadata configures, for example, how the component can be referenced in HTML and what services it requires.
|
||||
|
||||
Here's an example of basic metadata for `HeroListComponent`:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (metadata)" region="metadata"></code-example>
|
||||
|
||||
This example shows some of the most useful `@Component` configuration options:
|
||||
|
||||
* `selector`: A CSS selector that tells Angular to create and insert an instance of this component wherever it finds the corresponding tag in template HTML. For example, if an app's HTML contains `<app-hero-list></app-hero-list>`, then
|
||||
Angular inserts an instance of the `HeroListComponent` view between those tags.
|
||||
|
||||
* `templateUrl`: The module-relative address of this component's HTML template. Alternatively, you can provide the HTML template inline, as the value of the `template` property. This template defines the component's _host view_.
|
||||
|
||||
* `providers`: An array of **dependency injection providers** for services that the component requires. In the example, this tells Angular that the component's constructor requires a `HeroService` instance
|
||||
in order to get the list of heroes to display.
|
||||
|
||||
<hr/>
|
||||
|
||||
## Templates and views
|
||||
|
||||
<img src="generated/images/guide/architecture/template.png" alt="Template" class="left">
|
||||
|
||||
You define a component's view with its companion template. A template is a form of HTML that tells Angular how to render the component.
|
||||
|
||||
Views are typically arranged hierarchically, allowing you to modify or show and hide entire UI sections or pages as a unit. The template immediately associated with a component defines that component's _host view_. The component can also define a _view hierarchy_, which contains _embedded views_, hosted by other components.
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/component-tree.png" alt="Component tree" class="left">
|
||||
</figure>
|
||||
|
||||
A view hierarchy can include views from components in the same NgModule, but it also can (and often does) include views from components that are defined in different NgModules.
|
||||
|
||||
## Template syntax
|
||||
|
||||
A template looks like regular HTML, except that it also contains Angular [template syntax](guide/template-syntax), which alters the HTML based on your app's logic and the state of app and DOM data. Your template can use _data binding_ to coordinate the app and DOM data, _pipes_ to transform data before it is displayed, and _directives_ to apply app logic to what gets displayed.
|
||||
|
||||
For example, here is a template for the Tutorial's `HeroListComponent`:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.html" title="src/app/hero-list.component.html"></code-example>
|
||||
|
||||
This template uses typical HTML elements like `<h2>` and `<p>`, and also includes Angular template-syntax elements, `*ngFor`, `{{hero.name}}`, `(click)`, `[hero]`, and `<app-hero-detail>`. The template-syntax elements tell Angular how to render the HTML to the screen, using program logic and data.
|
||||
|
||||
* The `*ngFor` directive tells Angular to iterate over a list.
|
||||
|
||||
* The `{{hero.name}}`, `(click)`, and `[hero]` bind program data to and from the DOM, responding to user input. See more about [data binding](#data-binding) below.
|
||||
|
||||
* The `<app-hero-detail>` tag in the example is an element that represents a new component, `HeroDetailComponent`. The `HeroDetailComponent` (code not shown) is a child component of the `HeroListComponent` that defines the Hero-detail view. Notice how custom components like this mix seamlessly with native HTML in the same layouts.
|
||||
|
||||
### Data binding
|
||||
|
||||
Without a framework, you would be responsible for pushing data values into the HTML controls and turning user responses into actions and value updates. Writing such push/pull logic by hand is tedious, error-prone, and a nightmare to read, as any experienced jQuery programmer can attest.
|
||||
|
||||
Angular supports *two-way data binding*, a mechanism for coordinating parts of a template with parts of a component. Add binding markup to the template HTML to tell Angular how to connect both sides.
|
||||
|
||||
The following diagram shows the four forms of data binding markup. Each form has a direction—to the DOM, from the DOM, or in both directions.
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/databinding.png" alt="Data Binding" class="left">
|
||||
</figure>
|
||||
|
||||
This example from the `HeroListComponent` template uses three of these forms:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (binding)" region="binding"></code-example>
|
||||
|
||||
* The `{{hero.name}}` [*interpolation*](guide/displaying-data#interpolation)
|
||||
displays the component's `hero.name` property value within the `<li>` element.
|
||||
|
||||
`{{hero.name}}`[*插值表达式*](guide/displaying-data#interpolation)在 `<li>` 标签中显示组件的 `hero.name` 属性的值。
|
||||
|
||||
* The `[hero]` [*property binding*](guide/template-syntax#property-binding) passes the value of `selectedHero` from
|
||||
the parent `HeroListComponent` to the `hero` property of the child `HeroDetailComponent`.
|
||||
|
||||
`[hero]`[*属性绑定*](guide/template-syntax#property-binding)把父组件 `HeroListComponent` 的 `selectedHero` 的值传到子组件 `HeroDetailComponent` 的 `hero` 属性中。
|
||||
|
||||
* The `(click)` [*event binding*](guide/user-input#binding-to-user-input-events) calls the component's `selectHero` method when the user clicks a hero's name.
|
||||
|
||||
**Two-way data binding** is an important fourth form that combines property and event binding in a single notation. Here's an example from the `HeroDetailComponent` template that uses two-way data binding with the `ngModel` directive:
|
||||
|
||||
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
|
||||
|
||||
In two-way binding, a data property value flows to the input box from the component as with property binding.
|
||||
The user's changes also flow back to the component, resetting the property to the latest value,
|
||||
as with event binding.
|
||||
|
||||
在双向绑定中,数据属性值通过属性绑定从组件流到输入框。用户的修改通过事件绑定流回组件,把属性值设置为最新的值。
|
||||
|
||||
Angular processes *all* data bindings once per JavaScript event cycle,
|
||||
from the root of the application component tree through all child components.
|
||||
|
||||
Angular 在每个 JavaScript 事件循环中处理*所有的*数据绑定,它会从组件树的根部开始,递归处理全部子组件。
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/component-databinding.png" alt="Data Binding" class="left">
|
||||
</figure>
|
||||
|
||||
Data binding plays an important role in communication between a template and its component, and is also important for communication between parent and child components.
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/parent-child-binding.png" alt="Parent/Child binding" class="left">
|
||||
</figure>
|
||||
|
||||
### Pipes
|
||||
|
||||
### 管道
|
||||
|
||||
Angular pipes let you declare display-value transformations in your template HTML. A class with the `@Pipe` decorator defines a function that transforms input values to output values for display in a view.
|
||||
|
||||
Angular defines various pipes, such as the [date](https://angular.io/api/common/DatePipe) pipe and [currency](https://angular.io/api/common/CurrencyPipe) pipe; for a complete list, see the [Pipes API list](https://angular.io/api?type=pipe). You can also define new pipes.
|
||||
|
||||
To specify a value transformation in an HTML template, use the [pipe operator (|)](https://angular.io/guide/template-syntax#pipe):
|
||||
|
||||
`{{interpolated_value | pipe_name}}`
|
||||
|
||||
You can chain pipes, sending the output of one pipe function to be transformed by another pipe function. A pipe can also take arguments that control how it performs its transformation. For example, you can pass the desired format to the `date` pipe:
|
||||
|
||||
```
|
||||
|
||||
<!-- Default format: output 'Jun 15, 2015'-->
|
||||
|
||||
<p>Today is {{today | date}}</p>
|
||||
|
||||
<!-- fullDate format: output 'Monday, June 15, 2015'-->
|
||||
|
||||
<p>The date is {{today | date:'fullDate'}}</p>
|
||||
|
||||
<!-- shortTime format: output '9:43 AM'-->
|
||||
|
||||
<p>The time is {{today | date:'shortTime'}}</p>
|
||||
|
||||
```
|
||||
|
||||
<hr/>
|
||||
|
||||
### Directives
|
||||
|
||||
<img src="generated/images/guide/architecture/directive.png" alt="Directives" class="left">
|
||||
|
||||
Angular templates are *dynamic*. When Angular renders them, it transforms the DOM according to the instructions given by *directives*. A directive is a class with a `@Directive` decorator.
|
||||
|
||||
A component is technically a directive - but components are so distinctive and central to Angular applications that Angular defines the `@Component` decorator, which extends the `@Directive` decorator with template-oriented features.
|
||||
|
||||
There are two kinds of directives besides components: _structural_ and _attribute_ directives. Just as for components, the metadata for a directive associates the class with a `selector` that you use to insert it into HTML. In templates, directives typically appear within an element tag as attributes, either by name or as the target of an assignment or a binding.
|
||||
|
||||
#### Structural directives
|
||||
|
||||
Structural directives alter layout by adding, removing, and replacing elements in DOM. The example template uses two built-in structural directives to add application logic to how the view is rendered:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (structural)" region="structural"></code-example>
|
||||
|
||||
* [`*ngFor`](guide/displaying-data#ngFor) is an iterative; it tells Angular to stamp out one `<li>` per hero in the `heroes` list.
|
||||
|
||||
* [`*ngIf`](guide/displaying-data#ngIf) is a conditional; it includes the `HeroDetail` component only if a selected hero exists.
|
||||
|
||||
#### Attribute directives
|
||||
|
||||
Attribute directives alter the appearance or behavior of an existing element.
|
||||
In templates they look like regular HTML attributes, hence the name.
|
||||
|
||||
The `ngModel` directive, which implements two-way data binding, is an example of an attribute directive. `ngModel` modifies the behavior of an existing element (typically an `<input>`) by setting its display value property and responding to change events.
|
||||
|
||||
`ngModel` 指令就是属性型指令的一个例子,它实现了双向数据绑定。
|
||||
`ngModel` 修改现有元素(一般是 `<input>`)的行为:设置其显示属性值,并响应 change 事件。
|
||||
|
||||
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
|
||||
|
||||
Angular has more pre-defined directives that either alter the layout structure
|
||||
(for example, [ngSwitch](guide/template-syntax#ngSwitch))
|
||||
or modify aspects of DOM elements and components
|
||||
(for example, [ngStyle](guide/template-syntax#ngStyle) and [ngClass](guide/template-syntax#ngClass)).
|
||||
|
||||
You can also write your own directives. Components such as `HeroListComponent` are one kind of custom directive. You can also create custom structural and attribute directives.
|
||||
|
||||
<!-- PENDING: link to where to learn more about other kinds! -->
|
123
aio/content/guide/architecture-modules.md
Normal file
123
aio/content/guide/architecture-modules.md
Normal file
@ -0,0 +1,123 @@
|
||||
# Introduction to modules
|
||||
|
||||
<img src="generated/images/guide/architecture/module.png" alt="Module" class="left">
|
||||
|
||||
Angular apps are modular and Angular has its own modularity system called _NgModules_. An NgModule is a container for a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities. It can contain components, service providers, and other code files whose scope is defined by the containing NgModule. It can import functionality that is exported from other NgModules, and export selected functionality for use by other NgModules.
|
||||
|
||||
Every Angular app has at least one NgModule class, [the _root module_](guide/bootstrapping), which is conventionally named `AppModule` and resides in a file named `app.module.ts`. You launch your app by *bootstrapping* the root NgModule.
|
||||
|
||||
While a small application might have only one NgModule, most apps have many more _feature modules_. The _root_ NgModule for an app is so named because it can include child NgModules in a hierarchy of any depth.
|
||||
|
||||
## NgModule metadata
|
||||
|
||||
An NgModule is defined as a class decorated with `@NgModule`. The `@NgModule` decorator is a function that takes a single metadata object, whose properties describe the module. The most important properties are as follows.
|
||||
|
||||
* `declarations`—The [components](guide/architecture-components), _directives_, and _pipes_ that belong to this NgModule.
|
||||
|
||||
* `exports`—The subset of declarations that should be visible and usable in the _component templates_ of other NgModules.
|
||||
|
||||
* `imports`—Other modules whose exported classes are needed by component templates declared in _this_ NgModule.
|
||||
|
||||
* `providers`—Creators of [services](guide/architecture-services) that this NgModule contributes to the global collection of services; they become accessible in all parts of the app. (You can also specify providers at the component level, which is often preferred.)
|
||||
|
||||
* `bootstrap`—The main application view, called the _root component_, which hosts all other app views. Only the _root NgModule_ should set this `bootstrap` property.
|
||||
|
||||
Here's a simple root NgModule definition:
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="module" title="src/app/app.module.ts" linenums="false"></code-example>
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
The `export` of `AppComponent` is just to show how to export; it isn't actually necessary in this example. A root NgModule has no reason to _export_ anything because other modules don't need to _import_ the root NgModule.
|
||||
|
||||
</div>
|
||||
|
||||
## NgModules and components
|
||||
|
||||
NgModules provide a _compilation context_ for their components. A root NgModule always has a root component that is created during bootstrap, but any NgModule can include any number of additional components, which can be loaded through the router or created through the template. The components that belong to an NgModule share a compilation context.
|
||||
|
||||
<figure>
|
||||
|
||||
<img src="generated/images/guide/architecture/compilation-context.png" alt="Component compilation context" class="left">
|
||||
|
||||
</figure>
|
||||
|
||||
<br class="clear">
|
||||
|
||||
A component and its template together define a _view_. A component can contain a _view hierarchy_, which allows you to define arbitrarily complex areas of the screen that can be created, modified, and destroyed as a unit. A view hierarchy can mix views defined in components that belong to different NgModules. This is often the case, especially for UI libraries.
|
||||
|
||||
<figure>
|
||||
|
||||
<img src="generated/images/guide/architecture/view-hierarchy.png" alt="View hierarchy" class="left">
|
||||
|
||||
</figure>
|
||||
|
||||
<br class="clear">
|
||||
|
||||
When you create a component, it is associated directly with a single view, called the _host view_. The host view can be the root of a view hierarchy, which can contain _embedded views_, which are in turn the host views of other components. Those components can be in the same NgModule, or can be imported from other NgModules. Views in the tree can be nested to any depth.
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
The hierarchical structure of views is a key factor in the way Angular detects and responds to changes in the DOM and app data.
|
||||
|
||||
</div>
|
||||
|
||||
## NgModules and JavaScript modules
|
||||
|
||||
The NgModule system is different from and unrelated to the JavaScript (ES2015) module system for managing collections of JavaScript objects. These are two different and _complementary_ module systems. You can use them both to write your apps.
|
||||
|
||||
In JavaScript each _file_ is a module and all objects defined in the file belong to that module.
|
||||
The module declares some objects to be public by marking them with the `export` key word.
|
||||
Other JavaScript modules use *import statements* to access public objects from other modules.
|
||||
|
||||
JavaScript 中,每个*文件*是一个模块,文件中定义的所有对象都从属于那个模块。
|
||||
通过 `export` 关键字,模块可以把它的某些对象声明为公共的。
|
||||
其它 JavaScript 模块可以使用*import 语句*来访问这些公共对象。
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" region="imports" linenums="false"></code-example>
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" region="export" linenums="false"></code-example>
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
<a href="http://exploringjs.com/es6/ch_modules.html">Learn more about the JavaScript module system on the web.</a>
|
||||
|
||||
<a href="http://exploringjs.com/es6/ch_modules.html">学习更多关于 JavaScript 模块的知识。</a>
|
||||
|
||||
</div>
|
||||
|
||||
## Angular libraries
|
||||
|
||||
<img src="generated/images/guide/architecture/library-module.png" alt="Component" class="left">
|
||||
|
||||
Angular ships as a collection of JavaScript modules. You can think of them as library modules. Each Angular library name begins with the `@angular` prefix. Install them with the `npm` package manager and import parts of them with JavaScript `import` statements.
|
||||
|
||||
<br class="clear">
|
||||
|
||||
For example, import Angular's `Component` decorator from the `@angular/core` library like this:
|
||||
|
||||
例如,象下面这样,从 `@angular/core` 库中导入 `Component` 装饰器:
|
||||
|
||||
<code-example path="architecture/src/app/app.component.ts" region="import" linenums="false"></code-example>
|
||||
|
||||
You also import NgModules from Angular _libraries_ using JavaScript import statements:
|
||||
|
||||
还可以使用 JavaScript 的导入语句从 Angular *库*中导入 Angular *模块*:
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="import-browser-module" linenums="false"></code-example>
|
||||
|
||||
In the example of the simple root module above, the application module needs material from within the `BrowserModule`. To access that material, add it to the `@NgModule` metadata `imports` like this.
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="ngmodule-imports" linenums="false"></code-example>
|
||||
|
||||
In this way you're using both the Angular and JavaScript module systems _together_. Although it's easy to confuse the two systems, which share the common vocabulary of "imports" and "exports", you will become familiar with the different contexts in which they are used.
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
Learn more from the [NgModules](guide/ngmodules) page.
|
||||
|
||||
更多信息,参见 [NgModules](guide/ngmodules)。
|
||||
|
||||
</div>
|
||||
|
||||
<hr/>
|
48
aio/content/guide/architecture-next-steps.md
Normal file
48
aio/content/guide/architecture-next-steps.md
Normal file
@ -0,0 +1,48 @@
|
||||
# Next steps: tools and techniques
|
||||
|
||||
Once you have understood the basic building blocks, you can begin to learn more about the features and tools that are available to help you develop and deliver Angular applications. Angular provides a lot more features and services that are covered in this documentation.
|
||||
|
||||
#### Responsive programming tools
|
||||
|
||||
* [Lifecycle hooks](guide/lifecycle-hooks): Tap into key moments in the lifetime of a component, from its creation to its destruction, by implementing the lifecycle hook interfaces.
|
||||
|
||||
* [Observables and event processing](guide/observables): How to use observables with components and services to publish and subscribe to messages of any type, such as user-interaction events and asynchronous operation results.
|
||||
|
||||
#### Client-server interaction tools
|
||||
|
||||
* [HTTP](guide/http): Communicate with a server to get data, save data, and invoke server-side actions with an HTTP client.
|
||||
|
||||
* [Server-side Rendering](guide/universal): Angular Universal generates static application pages on the server through server-side rendering (SSR). This allows you to run your Angular app on the server in order to improve performance and show the first page quickly on mobile and low-powered devices, and also facilitate web crawlers.
|
||||
|
||||
* [Service Workers](guide/service-worker-intro): A service worker is a script that runs in the web browser and manages caching for an application. Service workers function as a network proxy. They intercept outgoing HTTP requests and can, for example, deliver a cached response if one is available. You can significantly improve the user experience by using a service worker to reduce dependency on the network.
|
||||
|
||||
#### Domain-specific libraries
|
||||
|
||||
* [Animations](guide/animations): Animate component behavior
|
||||
without deep knowledge of animation techniques or CSS with Angular's animation library.
|
||||
|
||||
* [Forms](guide/forms): Support complex data entry scenarios with HTML-based validation and dirty checking.
|
||||
|
||||
#### Support for the development cycle
|
||||
|
||||
* [Testing Platform](guide/testing): Run unit tests on your application parts as they interact with the Angular framework.
|
||||
|
||||
* [Internationalization](guide/i18n): Angular's internationalization (i18n) tools can help you make your app available in multiple languages.
|
||||
|
||||
* [Compilation](guide/aot-compiler): Angular provides just-in-time (JIT) compilation for the development environment, and ahead-of-time (AOT) compilation for the production environment.
|
||||
|
||||
* [Security guidelines](guide/security): Learn about Angular's built-in protections against common web-app vulnerabilities and attacks such as cross-site scripting attacks.
|
||||
|
||||
#### Setup and deployment tools
|
||||
|
||||
* [Setup for local development](guide/setup): Learn how to set up a new project for development with QuickStart.
|
||||
|
||||
* [Installation](guide/npm-packages): The [Angular CLI](https://cli.angular.io/), Angular applications, and Angular itself depend on features and functionality provided by libraries that are available as [npm](https://docs.npmjs.com/) packages.
|
||||
|
||||
* [Typescript Configuration](guide/typescript-configuration): TypeScript is the primary language for Angular application development.
|
||||
|
||||
* [Browser support](guide/browser-support): Learn how to make your apps compatible across a wide range of browsers.
|
||||
|
||||
* [Deployment](guide/deployment): Learn techniques for deploying your Angular application to a remote server.
|
||||
|
||||
<hr/>
|
81
aio/content/guide/architecture-services.md
Normal file
81
aio/content/guide/architecture-services.md
Normal file
@ -0,0 +1,81 @@
|
||||
# Introduction to services and dependency injection
|
||||
|
||||
<img src="generated/images/guide/architecture/service.png" alt="Service" class="left">
|
||||
|
||||
_Service_ is a broad category encompassing any value, function, or feature that an app needs. A service is typically a class with a narrow, well-defined purpose. It should do something specific and do it well.
|
||||
|
||||
<br class="clear">
|
||||
|
||||
Angular distinguishes components from services in order to increase modularity and reusability.
|
||||
|
||||
* By separating a component's view-related functionality from other kinds of processing, you can make your component classes lean and efficient. Ideally, a component's job is to enable the user experience and nothing more. It should present properties and methods for data binding, in order to mediate between the view (rendered by the template) and the application logic (which often includes some notion of a _model_).
|
||||
|
||||
* A component should not need to define things like how to fetch data from the server, validate user input, or log directly to the console. Instead, it can delegate such tasks to services. By defining that kind of processing task in an injectable service class, you make it available to any component. You can also make your app more adaptable by injecting different providers of the same kind of service, as appropriate in different circumstances.
|
||||
|
||||
Angular doesn't *enforce* these principles. Angular does help you *follow* these principles by making it easy to factor your
|
||||
application logic into services and make those services available to components through *dependency injection*.
|
||||
|
||||
## Service examples
|
||||
|
||||
Here's an example of a service class that logs to the browser console:
|
||||
|
||||
下面是一个服务类的范例,用于把日志记录到浏览器的控制台:
|
||||
|
||||
<code-example path="architecture/src/app/logger.service.ts" linenums="false" title="src/app/logger.service.ts (class)" region="class"></code-example>
|
||||
|
||||
Services can depend on other services. For example, here's a `HeroService` that depends on the `Logger` service, and also uses `BackendService` to get heroes. That service in turn might depend on the `HttpClient` service to fetch heroes asynchronously from a server.
|
||||
|
||||
<code-example path="architecture/src/app/hero.service.ts" linenums="false" title="src/app/hero.service.ts (class)" region="class"></code-example>
|
||||
|
||||
<hr/>
|
||||
|
||||
## Dependency injection
|
||||
|
||||
## 依赖注入(dependency injection)
|
||||
|
||||
<img src="generated/images/guide/architecture/dependency-injection.png" alt="Service" class="left">
|
||||
|
||||
Components consume services; that is, you can *inject* a service into a component, giving the component access to that service class.
|
||||
|
||||
To define a class as a service in Angular, use the `@Injectable` decorator to provide the metadata that allows Angular to inject it into a component as a *dependency*.
|
||||
|
||||
Similarly, use the `@Injectable` decorator to indicate that a component or other class (such as another service, a pipe, or an NgModule) _has_ a dependency. A dependency doesn't have to be a service—it could be a function, for example, or a value.
|
||||
|
||||
*Dependency injection* (often called DI) is wired into the Angular framework and used everywhere to provide new components with the services or other things they need.
|
||||
|
||||
* The *injector* is the main mechanism. You don't have to create an Angular injector. Angular creates an application-wide injector for you during the bootstrap process.
|
||||
|
||||
* The injector maintains a *container* of dependency instances that it has already created, and reuses them if possible.
|
||||
|
||||
* A *provider* is a recipe for creating a dependency. For a service, this is typically the service class itself. For any dependency you need in your app, you must register a provider with the app's injector, so that the injector can use it to create new instances.
|
||||
|
||||
When Angular creates a new instance of a component class, it determines which services or other dependencies that component needs by looking at the types of its constructor parameters. For example, the constructor of `HeroListComponent` needs a `HeroService`:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (constructor)" region="ctor"></code-example>
|
||||
|
||||
When Angular discovers that a component depends on a service, it first checks if the injector already has any existing instances of that service. If a requested service instance does not yet exist, the injector makes one using the registered provider, and adds it to the injector before returning the service to Angular.
|
||||
|
||||
When all requested services have been resolved and returned, Angular can call the component's constructor with those services as arguments.
|
||||
|
||||
The process of `HeroService` injection looks something like this:
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/injector-injects.png" alt="Service" class="left">
|
||||
</figure>
|
||||
|
||||
### Providing services
|
||||
|
||||
You must register at least one *provider* of any service you are going to use. You can register providers in modules or in components.
|
||||
|
||||
* When you add providers to the [root module](guide/architecture-modules), the same instance of a service is available to all components in your app.
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" linenums="false" title="src/app/app.module.ts (module providers)" region="providers"></code-example>
|
||||
|
||||
* When you register a provider at the component level, you get a new instance of the
|
||||
service with each new instance of that component. At the component level, register a service provider in the `providers` property of the `@Component` metadata:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (component providers)" region="providers"></code-example>
|
||||
|
||||
For more detailed information, see the [Dependency Injection](guide/dependency-injection) section.
|
||||
|
||||
<hr/>
|
@ -2,912 +2,164 @@
|
||||
|
||||
# 架构概览
|
||||
|
||||
Angular is a framework for building client applications in HTML and
|
||||
either JavaScript or a language like TypeScript that compiles to JavaScript.
|
||||
Angular is a platform and framework for building client applications in HTML and TypeScript.
|
||||
Angular is itself written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that you import into your apps.
|
||||
|
||||
Angular 是一个用 HTML 和 JavaScript 或者一个可以编译成 JavaScript 的语言(例如 Dart 或者 TypeScript ),来构建客户端应用的框架。
|
||||
Angular 是一个用 HTML 和 JavaScript 或者一个可以编译成 JavaScript 的语言(例如 Dart 或者 TypeScript ),来构建客户端应用的平台和框架。
|
||||
|
||||
The framework consists of several libraries, some of them core and some optional.
|
||||
The basic building blocks of an Angular application are _NgModules_, which provide a compilation context for _components_. NgModules collect related code into functional sets; an Angular app is defined by a set of NgModules. An app always has at least a _root module_ that enables bootstrapping, and typically has many more _feature modules_.
|
||||
|
||||
该框架包括一系列库,有些是核心库,有些是可选库。
|
||||
* Components define *views*, which are sets of screen elements that Angular can choose among and modify according to your program logic and data. Every app has at least a root component.
|
||||
|
||||
You write Angular applications by composing HTML *templates* with Angularized markup,
|
||||
writing *component* classes to manage those templates, adding application logic in *services*,
|
||||
and boxing components and services in *modules*.
|
||||
* Components use *services*, which provide specific functionality not directly related to views. Service providers can be *injected* into components as *dependencies*, making your code modular, reusable, and efficient.
|
||||
|
||||
你是这样编写 Angular 应用的:用 Angular 扩展语法编写 HTML *模板*,
|
||||
用*组件*类管理这些模板,用*服务*添加应用逻辑,
|
||||
用*模块*打包发布组件与服务。
|
||||
Both components and services are simply classes, with *decorators* that mark their type and provide metadata that tells Angular how to use them.
|
||||
|
||||
Then you launch the app by *bootstrapping* the _root module_.
|
||||
Angular takes over, presenting your application content in a browser and
|
||||
responding to user interactions according to the instructions you've provided.
|
||||
* The metadata for a component class associates it with a *template* that defines a view. A template combines ordinary HTML with Angular *directives* and *binding markup* that allow Angular to modify the HTML before rendering it for display.
|
||||
|
||||
然后,你通过*引导**根模块*来启动该应用。
|
||||
Angular 在浏览器中接管、展现应用的内容,并根据你提供的操作指令响应用户的交互。
|
||||
* The metadata for a service class provides the information Angular needs to make it available to components through *Dependency Injection (DI)*.
|
||||
|
||||
Of course, there is more to it than this.
|
||||
You'll learn the details in the pages that follow. For now, focus on the big picture.
|
||||
|
||||
当然,这只是冰山一角。后面你还会学到更多的细节。不过,目前还是先关注全景图吧。
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/overview2.png" alt="overview">
|
||||
</figure>
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
The code referenced on this page is available as a <live-example></live-example>.
|
||||
|
||||
本章所引用的代码见<live-example></live-example>。
|
||||
|
||||
</div>
|
||||
An app's components typically define many views, arranged hierarchically. Angular provides the `Router` service to help you define navigation paths among views. The router provides sophisticated in-browser navigational capabilities.
|
||||
|
||||
## Modules
|
||||
|
||||
## 模块
|
||||
|
||||
<img src="generated/images/guide/architecture/module.png" alt="Component" class="left">
|
||||
Angular defines the `NgModule`, which differs from and complements the JavaScript (ES2015) module. An NgModule declares a compilation context for a set of components that is dedicated to an application domain, a workflow, or a closely related set of capabilities. An NgModule can associate its components with related code, such as services, to form functional units.
|
||||
|
||||
Angular apps are modular and Angular has its own modularity system called _NgModules_.
|
||||
Every Angular app has a _root module_, conventionally named `AppModule`, which provides the bootstrap mechanism that launches the application. An app typically contains many functional modules.
|
||||
|
||||
Angular 应用是模块化的,并且 Angular 有自己的模块系统,它被称为 *Angular 模块*或 *NgModules*。
|
||||
Like JavaScript modules, NgModules can import functionality from other NgModules, and allow their own functionality to be exported and used by other NgModules. For example, to use the router service in your app, you import the `Router` NgModule.
|
||||
|
||||
NgModules are a big deal.
|
||||
This page introduces modules; the [NgModules](guide/ngmodules) pages
|
||||
relating to NgModules covers them in detail.
|
||||
|
||||
NgModules 很重要。这里只是简单介绍,在 [NgModules](guide/ngmodules)中会做深入讲解。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
Every Angular app has at least one NgModule class, [the _root module_](guide/bootstrapping "Bootstrapping"),
|
||||
conventionally named `AppModule`.
|
||||
|
||||
每个 Angular 应用至少有一个模块([*根模块*](guide/bootstrapping "引导启动")),习惯上命名为 `AppModule`。
|
||||
|
||||
While the _root module_ may be the only module in a small application, most apps have many more
|
||||
_feature modules_, each a cohesive block of code dedicated to an application domain,
|
||||
a workflow, or a closely related set of capabilities.
|
||||
|
||||
*根模块*在一些小型应用中可能是唯一的模块,大多数应用会有很多*特性模块*,每个模块都是一个内聚的代码块专注于某个应用领域、工作流或紧密相关的功能。
|
||||
|
||||
An NgModule, whether a _root_ or _feature_, is a class with an `@NgModule` decorator.
|
||||
|
||||
Angular 模块(无论是*根模块*还是*特性模块*)都是一个带有 `@NgModule` 装饰器的类。
|
||||
Organizing your code into distinct functional modules helps in managing development of complex applications, and in designing for reusability. In addition, this technique lets you take advantage of _lazy-loading_—that is, loading modules on demand—in order to minimize the amount of code that needs to be loaded at startup.
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
Decorators are functions that modify JavaScript classes.
|
||||
Angular has many decorators that attach metadata to classes so that it knows
|
||||
what those classes mean and how they should work.
|
||||
<a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0">
|
||||
Learn more</a> about decorators on the web.
|
||||
|
||||
装饰器是用来修饰 JavaScript 类的函数。
|
||||
Angular 有很多装饰器,它们负责把元数据附加到类上,以了解那些类的设计意图以及它们应如何工作。
|
||||
关于装饰器的<a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0" target="_blank">更多信息</a>。
|
||||
For a more detailed discussion, see [Introduction to modules](guide/architecture-modules).
|
||||
|
||||
</div>
|
||||
|
||||
`NgModule` is a decorator function that takes a single metadata object whose properties describe the module.
|
||||
The most important properties are:
|
||||
|
||||
`NgModule` 是一个装饰器函数,它接收一个用来描述模块属性的元数据对象。其中最重要的属性是:
|
||||
|
||||
* `declarations` - the _view classes_ that belong to this module.
|
||||
Angular has three kinds of view classes: [components](guide/architecture#components), [directives](guide/architecture#directives), and [pipes](guide/pipes).
|
||||
|
||||
`declarations` - 声明本模块中拥有的*视图类*。Angular 有三种视图类:[组件](guide/architecture#components)、[指令](guide/architecture#directives)和[管道](guide/pipes)。
|
||||
|
||||
* `exports` - the subset of declarations that should be visible and usable in the component [templates](guide/architecture#templates) of other modules.
|
||||
|
||||
`exports` - declarations 的子集,可用于其它模块的组件[模板](guide/architecture#templates)。
|
||||
|
||||
* `imports` - other modules whose exported classes are needed by component templates declared in _this_ module.
|
||||
|
||||
`imports` - *本*模块声明的组件模板需要的类所在的其它模块。
|
||||
|
||||
* `providers` - creators of [services](guide/architecture#services) that this module contributes to
|
||||
the global collection of services; they become accessible in all parts of the app.
|
||||
|
||||
`providers` - [服务](guide/architecture#services)的创建者,并加入到全局服务列表中,可用于应用任何部分。
|
||||
|
||||
* `bootstrap` - the main application view, called the _root component_,
|
||||
that hosts all other app views. Only the _root module_ should set this `bootstrap` property.
|
||||
|
||||
`bootstrap` - 指定应用的主视图(称为*根组件*),它是所有其它视图的宿主。只有*根模块*才能设置 `bootstrap` 属性。
|
||||
|
||||
Here's a simple root module:
|
||||
|
||||
下面是一个简单的根模块:
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="module" title="src/app/app.module.ts" linenums="false"></code-example>
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
The `export` of `AppComponent` is just to show how to use the `exports` array to export a component; it isn't actually necessary in this example. A root module has no reason to _export_ anything because other components don't need to _import_ the root module.
|
||||
|
||||
`AppComponent` 的 `export` 语句只是用于演示如何导出的,它在这个例子中并不是必须的。根模块不需要*导出*任何东西,因为其它组件不需要导入根模块。
|
||||
|
||||
</div>
|
||||
|
||||
Launch an application by _bootstrapping_ its root module.
|
||||
During development you're likely to bootstrap the `AppModule` in a `main.ts` file like this one.
|
||||
|
||||
通过*引导*根模块来启动应用。
|
||||
在开发期间,你通常在一个 `main.ts` 文件中引导 `AppModule`,就像这样:
|
||||
|
||||
<code-example path="architecture/src/main.ts" title="src/main.ts" linenums="false"></code-example>
|
||||
|
||||
### NgModules vs. JavaScript modules
|
||||
|
||||
### NgModules vs. JavaScript 模块
|
||||
|
||||
The NgModule — a class decorated with `@NgModule` — is a fundamental feature of Angular.
|
||||
|
||||
NgModule(一个带 `@NgModule` 装饰器的类)是 Angular 的基础特性之一。
|
||||
|
||||
JavaScript also has its own module system for managing collections of JavaScript objects.
|
||||
It's completely different and unrelated to the NgModule system.
|
||||
|
||||
JavaScript 也有自己的模块系统,用来管理一组 JavaScript 对象。
|
||||
它与 Angular 的模块系统完全不同且完全无关。
|
||||
|
||||
In JavaScript each _file_ is a module and all objects defined in the file belong to that module.
|
||||
The module declares some objects to be public by marking them with the `export` key word.
|
||||
Other JavaScript modules use *import statements* to access public objects from other modules.
|
||||
|
||||
JavaScript 中,每个*文件*是一个模块,文件中定义的所有对象都从属于那个模块。
|
||||
通过 `export` 关键字,模块可以把它的某些对象声明为公共的。
|
||||
其它 JavaScript 模块可以使用*import 语句*来访问这些公共对象。
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" region="imports" linenums="false"></code-example>
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" region="export" linenums="false"></code-example>
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
<a href="http://exploringjs.com/es6/ch_modules.html">Learn more about the JavaScript module system on the web.</a>
|
||||
|
||||
<a href="http://exploringjs.com/es6/ch_modules.html">学习更多关于 JavaScript 模块的知识。</a>
|
||||
|
||||
</div>
|
||||
|
||||
These are two different and _complementary_ module systems. Use them both to write your apps.
|
||||
|
||||
这两个模块化系统是不同但*互补*的,你在写程序时都会用到。
|
||||
|
||||
### Angular libraries
|
||||
|
||||
### Angular 模块库
|
||||
|
||||
<img src="generated/images/guide/architecture/library-module.png" alt="Component" class="left">
|
||||
|
||||
Angular ships as a collection of JavaScript modules. You can think of them as library modules.
|
||||
|
||||
Angular 提供了一组 JavaScript 模块。可以把它们看做库模块。
|
||||
|
||||
Each Angular library name begins with the `@angular` prefix.
|
||||
|
||||
每个 Angular 库的名字都带有 `@angular` 前缀。
|
||||
|
||||
You install them with the **npm** package manager and import parts of them with JavaScript `import` statements.
|
||||
|
||||
用 **npm** 包管理工具安装它们,用 JavaScript 的 `import` 语句导入其中某些部件。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
For example, import Angular's `Component` decorator from the `@angular/core` library like this:
|
||||
|
||||
例如,象下面这样,从 `@angular/core` 库中导入 `Component` 装饰器:
|
||||
|
||||
<code-example path="architecture/src/app/app.component.ts" region="import" linenums="false"></code-example>
|
||||
|
||||
You also import NgModules from Angular _libraries_ using JavaScript import statements:
|
||||
|
||||
还可以使用 JavaScript 的导入语句从 Angular *库*中导入 Angular *模块*:
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="import-browser-module" linenums="false"></code-example>
|
||||
|
||||
In the example of the simple root module above, the application module needs material from within that `BrowserModule`. To access that material, add it to the `@NgModule` metadata `imports` like this.
|
||||
|
||||
在上面那个简单的根模块的例子中,应用模块需要 `BrowserModule` 的某些素材。要访问这些素材,就得把它加入 `@NgModule` 元数据的 `imports` 中,就像这样:
|
||||
|
||||
<code-example path="architecture/src/app/mini-app.ts" region="ngmodule-imports" linenums="false"></code-example>
|
||||
|
||||
In this way you're using both the Angular and JavaScript module systems _together_.
|
||||
|
||||
这种情况下,你同时使用了 Angular 和 JavaScript 的模块化系统。
|
||||
|
||||
It's easy to confuse the two systems because they share the common vocabulary of "imports" and "exports".
|
||||
Hang in there. The confusion yields to clarity with time and experience.
|
||||
|
||||
这两个系统比较容易混淆,因为它们共享相同的词汇 “imports” 和 “exports”。不过没关系,先放一放,随着时间和经验的增长,自然就清楚了。
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
Learn more from the [NgModules](guide/ngmodules) page.
|
||||
|
||||
更多信息,参见 [NgModules](guide/ngmodules)。
|
||||
|
||||
</div>
|
||||
|
||||
<hr/>
|
||||
|
||||
## Components
|
||||
|
||||
## 组件
|
||||
|
||||
<img src="generated/images/guide/architecture/hero-component.png" alt="Component" class="left">
|
||||
Every Angular application has at least one component, the *root component* that connects a component hierarchy with the page DOM. Each component defines a class that contains application data and logic, and is associated with an HTML *template* that defines a view to be displayed in a target environment.
|
||||
|
||||
A _component_ controls a patch of screen called a *view*.
|
||||
|
||||
*组件*负责控制屏幕上的一小块区域叫做*视图*。
|
||||
|
||||
For example, the following views are controlled by components:
|
||||
|
||||
例如,下列视图都是由组件控制的:
|
||||
|
||||
* The app root with the navigation links.
|
||||
|
||||
带有导航链接的应用根组件。
|
||||
|
||||
* The list of heroes.
|
||||
|
||||
英雄列表。
|
||||
|
||||
* The hero editor.
|
||||
|
||||
英雄编辑器。
|
||||
|
||||
You define a component's application logic—what it does to support the view—inside a class.
|
||||
The class interacts with the view through an API of properties and methods.
|
||||
|
||||
你在类中定义组件的应用逻辑,为视图提供支持。
|
||||
组件通过一些由属性和方法组成的 API 与视图交互。
|
||||
|
||||
{@a component-code}
|
||||
|
||||
For example, this `HeroListComponent` has a `heroes` property that returns an array of heroes
|
||||
that it acquires from a service.
|
||||
`HeroListComponent` also has a `selectHero()` method that sets a `selectedHero` property when the user clicks to choose a hero from that list.
|
||||
|
||||
例如,`HeroListComponent` 有一个 `heroes` 属性,它返回一个英雄数组,这个数组从一个服务获得。
|
||||
`HeroListComponent` 还有一个当用户从列表中点选一个英雄时设置 `selectedHero` 属性的 `selectHero()` 方法。
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (class)" region="class"></code-example>
|
||||
|
||||
Angular creates, updates, and destroys components as the user moves through the application.
|
||||
Your app can take action at each moment in this lifecycle through optional [lifecycle hooks](guide/lifecycle-hooks), like `ngOnInit()` declared above.
|
||||
|
||||
当用户在这个应用中漫游时, Angular 会创建、更新和销毁组件。
|
||||
应用可以通过[生命周期钩子](guide/lifecycle-hooks)在组件生命周期的各个时间点上插入自己的操作,例如上面声明的 `ngOnInit()`。
|
||||
|
||||
<hr/>
|
||||
|
||||
## Templates
|
||||
|
||||
## 模板
|
||||
|
||||
<img src="generated/images/guide/architecture/template.png" alt="Template" class="left">
|
||||
|
||||
You define a component's view with its companion **template**. A template is a form of HTML
|
||||
that tells Angular how to render the component.
|
||||
|
||||
你通过组件的自带的**模板**来定义组件视图。模板以 HTML 形式存在,告诉 Angular 如何渲染组件。
|
||||
|
||||
A template looks like regular HTML, except for a few differences. Here is a
|
||||
template for our `HeroListComponent`:
|
||||
|
||||
多数情况下,模板看起来很像标准 HTML,当然也有一点不同的地方。下面是 `HeroListComponent` 组件的一个模板:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.html" title="src/app/hero-list.component.html"></code-example>
|
||||
|
||||
Although this template uses typical HTML elements like `<h2>` and `<p>`, it also has some differences. Code like `*ngFor`, `{{hero.name}}`, `(click)`, `[hero]`, and `<app-hero-detail>` uses Angular's [template syntax](guide/template-syntax).
|
||||
|
||||
模板除了可以使用像 `<h2>` 和 `<p>` 这样的典型的 HTML 元素,还能使用其它元素。
|
||||
例如,像 `*ngFor`、`{{hero.name}}`、`(click)`、`[hero]` 和 `<app-hero-detail>` 这样的代码使用了 Angular 的[模板语法](guide/template-syntax)。
|
||||
|
||||
In the last line of the template, the `<app-hero-detail>` tag is a custom element that represents a new component, `HeroDetailComponent`.
|
||||
|
||||
在模板的最后一行,`<app-hero-detail>` 标签就是一个用来表示新组件 `HeroDetailComponent` 的自定义元素。
|
||||
|
||||
The `HeroDetailComponent` is a *different* component than the `HeroListComponent` you've been reviewing.
|
||||
The `HeroDetailComponent` (code not shown) presents facts about a particular hero, the
|
||||
hero that the user selects from the list presented by the `HeroListComponent`.
|
||||
The `HeroDetailComponent` is a **child** of the `HeroListComponent`.
|
||||
|
||||
`HeroDetailComponent` 跟以前见到过的 `HeroListComponent` 是*不同*的组件。
|
||||
`HeroDetailComponent`(代码未显示)用于展现一个特定英雄的情况,这个英雄是用户从 `HeroListComponent` 列表中选择的。
|
||||
`HeroDetailComponent` 是 `HeroListComponent` 的*子组件*。
|
||||
|
||||
<img src="generated/images/guide/architecture/component-tree.png" alt="Metadata" class="left">
|
||||
|
||||
Notice how `<app-hero-detail>` rests comfortably among native HTML elements. Custom components mix seamlessly with native HTML in the same layouts.
|
||||
|
||||
注意到了吗?`<app-hero-detail>` 舒适地躺在原生 HTML 元素之间。
|
||||
自定义组件和原生 HTML 在同一布局中融合得天衣无缝。
|
||||
|
||||
<hr class="clear"/>
|
||||
|
||||
## Metadata
|
||||
|
||||
## 元数据
|
||||
|
||||
<img src="generated/images/guide/architecture/metadata.png" alt="Metadata" class="left">
|
||||
|
||||
Metadata tells Angular how to process a class.
|
||||
|
||||
元数据告诉 Angular 如何处理一个类。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
[Looking back at the code](guide/architecture#component-code) for `HeroListComponent`, you can see that it's just a class.
|
||||
There is no evidence of a framework, no "Angular" in it at all.
|
||||
|
||||
[回头看看](guide/architecture#component-code)`HeroListComponent` 就会明白:它只是一个类。
|
||||
一点框架的痕迹也没有,里面完全没有出现 "Angular" 的字样。
|
||||
|
||||
In fact, `HeroListComponent` really is *just a class*. It's not a component until you *tell Angular about it*.
|
||||
|
||||
实际上,`HeroListComponent` 真的*只是一个类*。直到你*告诉 Angular* 它是一个组件。
|
||||
|
||||
To tell Angular that `HeroListComponent` is a component, attach **metadata** to the class.
|
||||
|
||||
要告诉 Angular `HeroListComponent` 是个组件,只要把**元数据**附加到这个类。
|
||||
|
||||
In TypeScript, you attach metadata by using a **decorator**.
|
||||
Here's some metadata for `HeroListComponent`:
|
||||
|
||||
在 TypeScript 中,你要用**装饰器 (decorator) **来附加元数据。
|
||||
下面就是 `HeroListComponent` 的一些元数据。
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (metadata)" region="metadata"></code-example>
|
||||
|
||||
Here is the `@Component` decorator, which identifies the class
|
||||
immediately below it as a component class.
|
||||
|
||||
这里看到 `@Component` 装饰器,它把紧随其后的类标记成了组件类。
|
||||
|
||||
The `@Component` decorator takes a required configuration object with the
|
||||
information Angular needs to create and present the component and its view.
|
||||
|
||||
`@Component` 装饰器能接受一个配置对象, Angular 会基于这些信息创建和展示组件及其视图。
|
||||
|
||||
Here are a few of the most useful `@Component` configuration options:
|
||||
|
||||
`@Component` 的配置项包括:
|
||||
|
||||
* `selector`: CSS selector that tells Angular to create and insert an instance of this component
|
||||
where it finds a `<app-hero-list>` tag in *parent* HTML.
|
||||
For example, if an app's HTML contains `<app-hero-list></app-hero-list>`, then
|
||||
Angular inserts an instance of the `HeroListComponent` view between those tags.
|
||||
|
||||
`selector`: CSS 选择器,它告诉 Angular 在*父级* HTML 中查找 `<app-hero-list>` 标签,创建并插入该组件。
|
||||
例如,如果应用的 HTML 包含 `<app-hero-list></app-hero-list>`, Angular 就会把 `HeroListComponent` 的一个实例插入到这个标签中。
|
||||
|
||||
* `templateUrl`: module-relative address of this component's HTML template, shown [above](guide/architecture#templates).
|
||||
|
||||
`templateUrl`:组件 HTML 模板的模块相对地址,[如前所示](guide/architecture#templates)。
|
||||
|
||||
* `providers`: array of **dependency injection providers** for services that the component requires.
|
||||
This is one way to tell Angular that the component's constructor requires a `HeroService`
|
||||
so it can get the list of heroes to display.
|
||||
|
||||
`providers` - 组件所需服务的*依赖注入提供商*数组。
|
||||
这是在告诉 Angular:该组件的构造函数需要一个 `HeroService` 服务,这样组件就可以从服务中获得英雄数据。
|
||||
|
||||
<img src="generated/images/guide/architecture/template-metadata-component.png" alt="Metadata" class="left">
|
||||
|
||||
The metadata in the `@Component` tells Angular where to get the major building blocks you specify for the component.
|
||||
|
||||
`@Component` 里面的元数据会告诉 Angular 从哪里获取你为组件指定的主要的构建块。
|
||||
|
||||
The template, metadata, and component together describe a view.
|
||||
|
||||
模板、元数据和组件共同描绘出这个视图。
|
||||
|
||||
Apply other metadata decorators in a similar fashion to guide Angular behavior.
|
||||
`@Injectable`, `@Input`, and `@Output` are a few of the more popular decorators.
|
||||
|
||||
其它元数据装饰器用类似的方式来指导 Angular 的行为。
|
||||
例如 `@Injectable`、`@Input` 和 `@Output` 等是一些最常用的装饰器。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
The architectural takeaway is that you must add metadata to your code
|
||||
so that Angular knows what to do.
|
||||
|
||||
这种架构处理方式是:你向代码中添加元数据,以便 Angular 知道该怎么做。
|
||||
|
||||
<hr/>
|
||||
|
||||
## Data binding
|
||||
|
||||
## 数据绑定 (data binding)
|
||||
|
||||
Without a framework, you would be responsible for pushing data values into the HTML controls and turning user responses
|
||||
into actions and value updates. Writing such push/pull logic by hand is tedious, error-prone, and a nightmare to
|
||||
read as any experienced jQuery programmer can attest.
|
||||
|
||||
如果没有框架,你就得自己把数据值推送到 HTML 控件中,并把用户的反馈转换成动作和值更新。
|
||||
如果手工写代码来实现这些推/拉逻辑,肯定会枯燥乏味、容易出错,读起来简直是噩梦 —— 写过 jQuery 的程序员大概都对此深有体会。
|
||||
|
||||
<img src="generated/images/guide/architecture/databinding.png" alt="Data Binding" class="left">
|
||||
|
||||
Angular supports **data binding**,
|
||||
a mechanism for coordinating parts of a template with parts of a component.
|
||||
Add binding markup to the template HTML to tell Angular how to connect both sides.
|
||||
|
||||
Angular 支持**数据绑定**,一种让模板的各部分与组件的各部分相互合作的机制。
|
||||
往模板 HTML 中添加绑定标记,来告诉 Angular 如何把二者联系起来。
|
||||
|
||||
As the diagram shows, there are four forms of data binding syntax. Each form has a direction — to the DOM, from the DOM, or in both directions.
|
||||
|
||||
如图所示,数据绑定的语法有四种形式。每种形式都有一个方向 —— 绑定到 DOM 、绑定自 DOM 以及双向绑定。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
The `HeroListComponent` [example](guide/architecture#templates) template has three forms:
|
||||
|
||||
`HeroListComponent`[示例](guide/architecture#templates)模板中有三种形式:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (binding)" region="binding"></code-example>
|
||||
|
||||
* The `{{hero.name}}` [*interpolation*](guide/displaying-data#interpolation)
|
||||
displays the component's `hero.name` property value within the `<li>` element.
|
||||
|
||||
`{{hero.name}}`[*插值表达式*](guide/displaying-data#interpolation)在 `<li>` 标签中显示组件的 `hero.name` 属性的值。
|
||||
|
||||
* The `[hero]` [*property binding*](guide/template-syntax#property-binding) passes the value of `selectedHero` from
|
||||
the parent `HeroListComponent` to the `hero` property of the child `HeroDetailComponent`.
|
||||
|
||||
`[hero]`[*属性绑定*](guide/template-syntax#property-binding)把父组件 `HeroListComponent` 的 `selectedHero` 的值传到子组件 `HeroDetailComponent` 的 `hero` 属性中。
|
||||
|
||||
* The `(click)` [*event binding*](guide/user-input#click) calls the component's `selectHero` method when the user clicks a hero's name.
|
||||
|
||||
`(click)` [*事件绑定*](guide/user-input#click)在用户点击英雄的名字时调用组件的 `selectHero` 方法。
|
||||
|
||||
**Two-way data binding** is an important fourth form
|
||||
that combines property and event binding in a single notation, using the `ngModel` directive.
|
||||
Here's an example from the `HeroDetailComponent` template:
|
||||
|
||||
**双向数据绑定**是重要的第四种绑定形式,它使用 `ngModel` 指令组合了属性绑定和事件绑定的功能。
|
||||
下面是 `HeroDetailComponent` 模板的范例:
|
||||
|
||||
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
|
||||
|
||||
In two-way binding, a data property value flows to the input box from the component as with property binding.
|
||||
The user's changes also flow back to the component, resetting the property to the latest value,
|
||||
as with event binding.
|
||||
|
||||
在双向绑定中,数据属性值通过属性绑定从组件流到输入框。用户的修改通过事件绑定流回组件,把属性值设置为最新的值。
|
||||
|
||||
Angular processes *all* data bindings once per JavaScript event cycle,
|
||||
from the root of the application component tree through all child components.
|
||||
|
||||
Angular 在每个 JavaScript 事件循环中处理*所有的*数据绑定,它会从组件树的根部开始,递归处理全部子组件。
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/component-databinding.png" alt="Data Binding">
|
||||
</figure>
|
||||
|
||||
Data binding plays an important role in communication between a template and its component.
|
||||
|
||||
数据绑定在模板与对应组件的交互中扮演了重要的角色。
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/parent-child-binding.png" alt="Parent/Child binding">
|
||||
</figure>
|
||||
|
||||
Data binding is also important for communication between parent and child components.
|
||||
|
||||
数据绑定在父组件与子组件的通讯中也同样重要。
|
||||
|
||||
<hr/>
|
||||
|
||||
## Directives
|
||||
|
||||
## 指令
|
||||
|
||||
<img src="generated/images/guide/architecture/directive.png" alt="Parent child" class="left">
|
||||
|
||||
Angular templates are *dynamic*. When Angular renders them, it transforms the DOM
|
||||
according to the instructions given by **directives**.
|
||||
|
||||
Angular 模板是*动态的*。当 Angular 渲染它们时,它会根据**指令**提供的操作对 DOM 进行转换。
|
||||
|
||||
A directive is a class with a `@Directive` decorator.
|
||||
A component is a *directive-with-a-template*;
|
||||
a `@Component` decorator is actually a `@Directive` decorator extended with template-oriented features.
|
||||
|
||||
组件是一个*带模板的指令*;`@Component` 装饰器实际上就是一个 `@Directive` 装饰器,只是扩展了一些面向模板的特性。
|
||||
The `@Component` decorator identifies the class immediately below it as a component, and provides the template and related component-specific metadata.
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
While **a component is technically a directive**,
|
||||
components are so distinctive and central to Angular applications that this architectural overview separates components from directives.
|
||||
Decorators are functions that modify JavaScript classes. Angular defines a number of such decorators that attach specific kinds of metadata to classes, so that it knows what those classes mean and how they should work.
|
||||
|
||||
虽然**严格来说组件就是一个指令**,但是组件非常独特,并在 Angular 中位于中心地位,所以在架构概览中把组件从指令中独立了出来。
|
||||
<a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0">Learn more about decorators on the web.</a>
|
||||
|
||||
</div>
|
||||
|
||||
Two *other* kinds of directives exist: _structural_ and _attribute_ directives.
|
||||
### Templates, directives, and data binding
|
||||
|
||||
还有两种*其它*类型的指令:*结构型*指令和*属性 (attribute) 型*指令。
|
||||
A template combines HTML with Angular markup that can modify the HTML elements before they are displayed.
|
||||
Template *directives* provide program logic, and *binding markup* connects your application data and the document object model (DOM).
|
||||
|
||||
They tend to appear within an element tag as attributes do,
|
||||
sometimes by name but more often as the target of an assignment or a binding.
|
||||
* *Event binding* lets your app respond to user input in the target environment by updating your application data.
|
||||
|
||||
它们往往像属性 (attribute) 一样出现在元素标签中,
|
||||
偶尔会以名字的形式出现,但多数时候还是作为赋值目标或绑定目标出现。
|
||||
* *Property binding* lets you interpolate values that are computed from your application data into the HTML.
|
||||
|
||||
**Structural** directives alter layout by adding, removing, and replacing elements in DOM.
|
||||
Before a view is displayed, Angular evaluates the directives and resolves the binding syntax in the template to modify the HTML elements and the DOM, according to your program data and logic. Angular supports *two-way data binding*, meaning that changes in the DOM, such as user choices, can also be reflected back into your program data.
|
||||
|
||||
**结构型**指令通过在 DOM 中添加、移除和替换元素来修改布局。
|
||||
Your templates can also use *pipes* to improve the user experience by transforming values for display. Use pipes to display, for example, dates and currency values in a way appropriate to the user's locale. Angular provides predefined pipes for common transformations, and you can also define your own.
|
||||
|
||||
The [example template](guide/architecture#templates) uses two built-in structural directives:
|
||||
<div class="l-sub-section">
|
||||
|
||||
下面的[范例模板](guide/architecture#templates)中用到了两个内置的结构型指令:
|
||||
For a more detailed discussion of these concepts, see [Introduction to components](guide/architecture-components).
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.1.html" linenums="false" title="src/app/hero-list.component.html (structural)" region="structural"></code-example>
|
||||
</div>
|
||||
|
||||
* [`*ngFor`](guide/displaying-data#ngFor) tells Angular to stamp out one `<li>` per hero in the `heroes` list.
|
||||
{@a dependency-injection}
|
||||
|
||||
[`*ngFor`](guide/displaying-data#ngFor)告诉 Angular 为 `heroes` 列表中的每个英雄生成一个 `<li>` 标签。
|
||||
## Services and dependency injection
|
||||
|
||||
* [`*ngIf`](guide/displaying-data#ngIf) includes the `HeroDetail` component only if a selected hero exists.
|
||||
For data or logic that is not associated with a specific view, and that you want to share across components, you create a *service* class. A service class definition is immediately preceded by the `@Injectable` decorator. The decorator provides the metadata that allows your service to be *injected* into client components as a dependency.
|
||||
|
||||
[`*ngIf`](guide/displaying-data#ngIf)表示只有在选择的英雄存在时,才会包含 `HeroDetail` 组件。
|
||||
*Dependency injection* (or DI) lets you keep your component classes lean and efficient. They don't fetch data from the server, validate user input, or log directly to the console; they delegate such tasks to services.
|
||||
|
||||
**Attribute** directives alter the appearance or behavior of an existing element.
|
||||
In templates they look like regular HTML attributes, hence the name.
|
||||
<div class="l-sub-section">
|
||||
|
||||
**属性型** 指令修改一个现有元素的外观或行为。
|
||||
在模板中,它们看起来就像是标准的 HTML 属性,故名。
|
||||
For a more detailed discusssion, see [Introduction to services and DI](guide/architecture-services).
|
||||
|
||||
The `ngModel` directive, which implements two-way data binding, is
|
||||
an example of an attribute directive. `ngModel` modifies the behavior of
|
||||
an existing element (typically an `<input>`)
|
||||
by setting its display value property and responding to change events.
|
||||
</div>
|
||||
|
||||
`ngModel` 指令就是属性型指令的一个例子,它实现了双向数据绑定。
|
||||
`ngModel` 修改现有元素(一般是 `<input>`)的行为:设置其显示属性值,并响应 change 事件。
|
||||
### Routing
|
||||
|
||||
<code-example path="architecture/src/app/hero-detail.component.html" linenums="false" title="src/app/hero-detail.component.html (ngModel)" region="ngModel"></code-example>
|
||||
The Angular `Router` NgModule provides a service that lets you define a navigation path among the different application states and view hierarchies in your app. It is modeled on the familiar browser navigation conventions:
|
||||
|
||||
Angular has a few more directives that either alter the layout structure
|
||||
(for example, [ngSwitch](guide/template-syntax#ngSwitch))
|
||||
or modify aspects of DOM elements and components
|
||||
(for example, [ngStyle](guide/template-syntax#ngStyle) and [ngClass](guide/template-syntax#ngClass)).
|
||||
* Enter a URL in the address bar and the browser navigates to a corresponding page.
|
||||
|
||||
Angular 还有少量指令,它们或者修改结构布局(例如 [ngSwitch](guide/template-syntax#ngSwitch)),
|
||||
或者修改 DOM 元素和组件的各个方面(例如 [ngStyle](guide/template-syntax#ngStyle)和 [ngClass](guide/template-syntax#ngClass))。
|
||||
在地址栏输入 URL,浏览器就会导航到相应的页面。
|
||||
|
||||
Of course, you can also write your own directives. Components such as
|
||||
`HeroListComponent` are one kind of custom directive.
|
||||
* Click links on the page and the browser navigates to a new page.
|
||||
|
||||
当然,你也能编写自己的指令。像 `HeroListComponent` 这样的组件就是一种自定义指令。
|
||||
在页面中点击链接,浏览器就会导航到一个新页面。
|
||||
|
||||
<!-- PENDING: link to where to learn more about other kinds! -->
|
||||
* Click the browser's back and forward buttons and the browser navigates backward and forward through the history of pages you've seen.
|
||||
|
||||
点击浏览器的前进和后退按钮,浏览器就会在你的浏览历史中向前或向后导航。
|
||||
|
||||
The router maps URL-like paths to views instead of pages. When a user performs an action, such as clicking a link, that would load a new page in the browser, the router intercepts the browser's behavior, and shows or hides view hierarchies.
|
||||
|
||||
If the router determines that the current application state requires particular functionality, and the module that defines it has not been loaded, the router can _lazy-load_ the module on demand.
|
||||
|
||||
The router interprets a link URL according to your app's view navigation rules and data state. You can navigate to new views when the user clicks a button, selects from a drop box, or in response to some other stimulus from any source. The Router logs activity in the browser's history journal, so the back and forward buttons work as well.
|
||||
|
||||
To define navigation rules, you associate *navigation paths* with your components. A path uses a URL-like syntax that integrates your program data, in much the same way that template syntax integrates your views with your program data. You can then apply program logic to choose which views to show or to hide, in response to user input and your own access rules.
|
||||
|
||||
<div class="l-sub-section">
|
||||
|
||||
For a more detailed discussion, see [Routing and navigation](guide/router).
|
||||
|
||||
</div>
|
||||
|
||||
<hr/>
|
||||
|
||||
## Services
|
||||
## What's next
|
||||
|
||||
## 服务
|
||||
|
||||
<img src="generated/images/guide/architecture/service.png" alt="Service" class="left">
|
||||
|
||||
_Service_ is a broad category encompassing any value, function, or feature that your application needs.
|
||||
|
||||
*服务*是一个广义范畴,包括:值、函数,或应用所需的特性。
|
||||
|
||||
Almost anything can be a service.
|
||||
A service is typically a class with a narrow, well-defined purpose. It should do something specific and do it well.
|
||||
|
||||
几乎任何东西都可以是一个服务。
|
||||
典型的服务是一个类,具有专注的、明确的用途。它应该做一件特定的事情,并把它做好。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
Examples include:
|
||||
|
||||
例如:
|
||||
|
||||
* logging service
|
||||
|
||||
日志服务
|
||||
|
||||
* data service
|
||||
|
||||
数据服务
|
||||
|
||||
* message bus
|
||||
|
||||
消息总线
|
||||
|
||||
* tax calculator
|
||||
|
||||
税款计算器
|
||||
|
||||
* application configuration
|
||||
|
||||
应用程序配置
|
||||
|
||||
There is nothing specifically _Angular_ about services. Angular has no definition of a service.
|
||||
There is no service base class, and no place to register a service.
|
||||
|
||||
服务没有什么特别属于 *Angular* 的特性。 Angular 对于服务也没有什么定义。
|
||||
它甚至都没有定义服务的基类,也没有地方注册一个服务。
|
||||
|
||||
Yet services are fundamental to any Angular application. Components are big consumers of services.
|
||||
|
||||
即便如此,服务仍然是任何 Angular 应用的基础。组件就是最大的*服务*消费者。
|
||||
|
||||
Here's an example of a service class that logs to the browser console:
|
||||
|
||||
下面是一个服务类的范例,用于把日志记录到浏览器的控制台:
|
||||
|
||||
<code-example path="architecture/src/app/logger.service.ts" linenums="false" title="src/app/logger.service.ts (class)" region="class"></code-example>
|
||||
|
||||
Here's a `HeroService` that uses a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to fetch heroes.
|
||||
The `HeroService` depends on the `Logger` service and another `BackendService` that handles the server communication grunt work.
|
||||
|
||||
下面是 `HeroService` 类,用于获取英雄数据,并通过一个已解析的[承诺 (Promise)](http://exploringjs.com/es6/ch_promises.html) 返回它们。
|
||||
`HeroService` 还依赖于 `Logger` 服务和另一个用于处理服务器通讯的 `BackendService` 服务。
|
||||
|
||||
<code-example path="architecture/src/app/hero.service.ts" linenums="false" title="src/app/hero.service.ts (class)" region="class"></code-example>
|
||||
|
||||
Services are everywhere.
|
||||
|
||||
服务无处不在。
|
||||
|
||||
Component classes should be lean. They don't fetch data from the server,
|
||||
validate user input, or log directly to the console.
|
||||
They delegate such tasks to services.
|
||||
|
||||
组件类应保持精简。组件本身不从服务器获得数据、不进行验证输入,也不直接往控制台写日志。
|
||||
它们把这些任务委托给服务。
|
||||
|
||||
A component's job is to enable the user experience and nothing more. It mediates between the view (rendered by the template)
|
||||
and the application logic (which often includes some notion of a _model_).
|
||||
A good component presents properties and methods for data binding.
|
||||
It delegates everything nontrivial to services.
|
||||
|
||||
组件的任务就是提供用户体验,仅此而已。它介于视图(由模板渲染)和应用逻辑(通常包括*模型*的某些概念)之间。
|
||||
设计良好的组件为数据绑定提供属性和方法,把其它琐事都委托给服务。
|
||||
|
||||
Angular doesn't *enforce* these principles.
|
||||
It won't complain if you write a "kitchen sink" component with 3000 lines.
|
||||
|
||||
Angular 不会*强行保障*这些原则。
|
||||
即使你花 3000 行代码写了一个“厨房洗碗槽”组件,它也不会抱怨什么。
|
||||
|
||||
Angular does help you *follow* these principles by making it easy to factor your
|
||||
application logic into services and make those services available to components through *dependency injection*.
|
||||
|
||||
Angular 帮助你*遵循*这些原则 —— 它让你能轻易地把应用逻辑拆分到服务,并通过*依赖注入*来在组件中使用这些服务。
|
||||
|
||||
<hr/>
|
||||
|
||||
## Dependency injection
|
||||
|
||||
## 依赖注入(dependency injection)
|
||||
|
||||
<img src="generated/images/guide/architecture/dependency-injection.png" alt="Service" class="left">
|
||||
|
||||
_Dependency injection_ is a way to supply a new instance of a class
|
||||
with the fully-formed dependencies it requires. Most dependencies are services.
|
||||
Angular uses dependency injection to provide new components with the services they need.
|
||||
|
||||
“依赖注入”是提供类的新实例的一种方式,还负责处理好类所需的全部依赖。大多数依赖都是服务。
|
||||
Angular 使用依赖注入来提供新组件以及组件所需的服务。
|
||||
|
||||
<br class="clear">
|
||||
|
||||
Angular can tell which services a component needs by looking at the types of its constructor parameters.
|
||||
For example, the constructor of your `HeroListComponent` needs a `HeroService`:
|
||||
|
||||
Angular 通过查看构造函数的参数类型得知组件需要哪些服务。
|
||||
例如,`HeroListComponent` 组件的构造函数需要一个 `HeroService` 服务:
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (constructor)" region="ctor"></code-example>
|
||||
|
||||
When Angular creates a component, it first asks an **injector** for
|
||||
the services that the component requires.
|
||||
|
||||
当 Angular 创建组件时,会首先为组件所需的服务请求一个**注入器 (injector)**。
|
||||
|
||||
An injector maintains a container of service instances that it has previously created.
|
||||
If a requested service instance is not in the container, the injector makes one and adds it to the container
|
||||
before returning the service to Angular.
|
||||
When all requested services have been resolved and returned,
|
||||
Angular can call the component's constructor with those services as arguments.
|
||||
This is *dependency injection*.
|
||||
|
||||
注入器维护了一个服务实例的容器,存放着以前创建的实例。
|
||||
如果所请求的服务实例不在容器中,注入器就会创建一个服务实例,并且添加到容器中,然后把这个服务返回给 Angular。
|
||||
当所有请求的服务都被解析完并返回时,Angular 会以这些服务为参数去调用组件的构造函数。
|
||||
这就是*依赖注入* 。
|
||||
|
||||
The process of `HeroService` injection looks a bit like this:
|
||||
|
||||
`HeroService` 注入的过程差不多是这样的:
|
||||
You've learned the basics about the main building blocks of an Angular application. The following diagram shows how these basic pieces are related.
|
||||
|
||||
<figure>
|
||||
<img src="generated/images/guide/architecture/injector-injects.png" alt="Service">
|
||||
<img src="generated/images/guide/architecture/overview2.png" alt="overview">
|
||||
</figure>
|
||||
|
||||
If the injector doesn't have a `HeroService`, how does it know how to make one?
|
||||
* Together, a component and template define an Angular view.
|
||||
|
||||
如果注入器还没有 `HeroService`,它怎么知道该如何创建一个呢?
|
||||
* A decorator on a component class adds the metadata, including a pointer to the associated template.
|
||||
|
||||
In brief, you must have previously registered a **provider** of the `HeroService` with the injector.
|
||||
A provider is something that can create or return a service, typically the service class itself.
|
||||
* Directives and binding markup in a component's template modify views based on program data and logic.
|
||||
|
||||
简单点说,你必须先用注入器(injector)为 `HeroService` 注册一个**提供商(provider)**。
|
||||
提供商用来创建或返回服务,通常就是这个服务类本身(相当于 `new HeroService()`)。
|
||||
* The dependency injector provides services to a component, such as the router service that lets you define navigation among views.
|
||||
|
||||
You can register providers in modules or in components.
|
||||
Each of these subjects is introduced in more detail in the following pages.
|
||||
|
||||
你可以在模块中或组件中注册提供商。
|
||||
* [Modules](guide/architecture-modules)
|
||||
|
||||
In general, add providers to the [root module](guide/architecture#modules) so that
|
||||
the same instance of a service is available everywhere.
|
||||
* [Components](guide/architecture-components)
|
||||
|
||||
但通常会把提供商添加到[根模块](guide/architecture#modules)上,以便在任何地方都使用服务的同一个实例。
|
||||
* [Templates](guide/architecture-components#templates-and-views)
|
||||
|
||||
<code-example path="architecture/src/app/app.module.ts" linenums="false" title="src/app/app.module.ts (module providers)" region="providers"></code-example>
|
||||
* [Metadata](guide/architecture-components#component-metadata)
|
||||
|
||||
Alternatively, register at a component level in the `providers` property of the `@Component` metadata:
|
||||
* [Data binding](guide/architecture-components#data-binding)
|
||||
|
||||
或者,也可以在 `@Component` 元数据中的 `providers` 属性中把它注册在组件层:
|
||||
* [Directives](guide/architecture-components#directives)
|
||||
|
||||
<code-example path="architecture/src/app/hero-list.component.ts" linenums="false" title="src/app/hero-list.component.ts (component providers)" region="providers"></code-example>
|
||||
* [Pipes](guide/architecture-components#pipes)
|
||||
|
||||
Registering at a component level means you get a new instance of the
|
||||
service with each new instance of that component.
|
||||
* [Services and dependency injection](guide/architecture-services)
|
||||
|
||||
把它注册在组件级表示该组件的每一个新实例都会有一个服务的新实例。
|
||||
<div class="l-sub-section">
|
||||
|
||||
<!-- We've vastly oversimplified dependency injection for this overview.
|
||||
The full story is in the [dependency injection](guide/dependency-injection) page. -->
|
||||
Note that the code referenced on these pages is available as a <live-example></live-example>.
|
||||
|
||||
<!--在这个概览中,我们过度简化了依赖注入机制。
|
||||
详见[依赖注入](guide/dependency-injection)页 -->
|
||||
</div>
|
||||
|
||||
Points to remember about dependency injection:
|
||||
When you are familiar with these fundamental building blocks, you can explore them in more detail in the documentation. To learn about more tools and techniques that are available to help you build and deploy Angular applications, see [Next steps](guide/architecture-next-steps).
|
||||
|
||||
需要记住的关于依赖注入的要点是:
|
||||
|
||||
* Dependency injection is wired into the Angular framework and used everywhere.
|
||||
|
||||
依赖注入渗透在整个 Angular 框架中,被到处使用。
|
||||
|
||||
* The *injector* is the main mechanism.
|
||||
|
||||
**注入器 (injector)** 是本机制的核心。
|
||||
|
||||
* An injector maintains a *container* of service instances that it created.
|
||||
|
||||
注入器负责维护一个*容器*,用于存放它创建过的服务实例。
|
||||
|
||||
* An injector can create a new service instance from a *provider*.
|
||||
|
||||
注入器能使用*提供商*创建一个新的服务实例。
|
||||
|
||||
* A *provider* is a recipe for creating a service.
|
||||
|
||||
*提供商*是一个用于创建服务的配方。
|
||||
|
||||
* Register *providers* with injectors.
|
||||
|
||||
把*提供商*注册到注入器。
|
||||
|
||||
<hr/>
|
||||
|
||||
## Wrap up
|
||||
|
||||
## 总结
|
||||
|
||||
You've learned the basics about the eight main building blocks of an Angular application:
|
||||
|
||||
你学到的这些只是关于 Angular 应用程序的八个主要构造块的基础知识:
|
||||
|
||||
* [Modules](guide/architecture#modules)
|
||||
|
||||
[模块](guide/architecture#modules)
|
||||
|
||||
* [Components](guide/architecture#components)
|
||||
|
||||
[组件](guide/architecture#components)
|
||||
|
||||
* [Templates](guide/architecture#templates)
|
||||
|
||||
[模板](guide/architecture#templates)
|
||||
|
||||
* [Metadata](guide/architecture#metadata)
|
||||
|
||||
[元数据](guide/architecture#metadata)
|
||||
|
||||
* [Data binding](guide/architecture#data-binding)
|
||||
|
||||
[数据绑定](guide/architecture#data-binding)
|
||||
|
||||
* [Directives](guide/architecture#directives)
|
||||
|
||||
[指令](guide/architecture#directives)
|
||||
|
||||
* [Services](guide/architecture#services)
|
||||
|
||||
[服务](guide/architecture#services)
|
||||
|
||||
* [Dependency injection](guide/architecture#dependency-injection)
|
||||
|
||||
[依赖注入](guide/architecture#dependency-injection)
|
||||
|
||||
That's a foundation for everything else in an Angular application,
|
||||
and it's more than enough to get going.
|
||||
But it doesn't include everything you need to know.
|
||||
|
||||
这是 Angular 应用程序中所有其它东西的基础,要使用 Angular,以这些作为开端就绰绰有余了。
|
||||
但它仍然没有包含你需要知道的一切。
|
||||
|
||||
Here is a brief, alphabetical list of other important Angular features and services.
|
||||
Most of them are covered in this documentation (or soon will be).
|
||||
|
||||
这里是一个简短的、按字母排序的列表,列出了其它重要的 Angular 特性和服务。
|
||||
它们大多数已经(或即将)包括在这份开发文档中:
|
||||
|
||||
> [**Animations**](guide/animations): Animate component behavior
|
||||
without deep knowledge of animation techniques or CSS with Angular's animation library.
|
||||
|
||||
> [**动画**](guide/animations):用 Angular 的动画库让组件动起来,而不需要对动画技术或 CSS 有深入的了解。
|
||||
|
||||
> **Change detection**: The change detection documentation will cover how Angular decides that a component property value has changed,
|
||||
when to update the screen, and how it uses **zones** to intercept asynchronous activity and run its change detection strategies.
|
||||
|
||||
> **变更检测**:变更检测文档会告诉你 Angular 是如何决定组件的属性值变化,什么时候该更新到屏幕,
|
||||
以及它是如何利用**区域 (zone)** 来拦截异步活动并执行变更检测策略。
|
||||
|
||||
> **Events**: The events documentation will cover how to use components and services to raise events with mechanisms for
|
||||
publishing and subscribing to events.
|
||||
|
||||
> **事件**:事件文档会告诉你如何使用组件和服务触发支持发布和订阅的事件。
|
||||
|
||||
> [**Forms**](guide/forms): Support complex data entry scenarios with HTML-based validation and dirty checking.
|
||||
|
||||
> [**表单**](guide/forms):通过基于 HTML 的验证和脏检查机制支持复杂的数据输入场景。
|
||||
|
||||
> [**HTTP**](guide/http): Communicate with a server to get data, save data, and invoke server-side actions with an HTTP client.
|
||||
|
||||
> [**HTTP**](guide/http):通过 HTTP 客户端,可以与服务器通讯,以获得数据、保存数据和触发服务端动作。
|
||||
|
||||
> [**Lifecycle hooks**](guide/lifecycle-hooks): Tap into key moments in the lifetime of a component, from its creation to its destruction,
|
||||
by implementing the lifecycle hook interfaces.
|
||||
|
||||
> [**生命周期钩子**](guide/lifecycle-hooks):通过实现生命周期钩子接口,可以切入组件生命中的几个关键点:从创建到销毁。
|
||||
|
||||
> [**Pipes**](guide/pipes): Use pipes in your templates to improve the user experience by transforming values for display. Consider this `currency` pipe expression:
|
||||
>
|
||||
> [**管道**](guide/pipes):在模板中使用管道转换成用于显示的值,以增强用户体验。例如,`currency` 管道表达式:
|
||||
>
|
||||
> > `price | currency:'USD':true`
|
||||
>
|
||||
> It displays a price of 42.33 as `$42.33`.
|
||||
>
|
||||
> 它把价格“42.33”显示为 `$42.33`。
|
||||
>
|
||||
|
||||
> [**Router**](guide/router): Navigate from page to page within the client
|
||||
application and never leave the browser.
|
||||
|
||||
> [**路由器**](guide/router):在应用程序客户端的页面间导航,并且不离开浏览器。
|
||||
|
||||
> [**Testing**](guide/testing): Run unit tests on your application parts as they interact with the Angular framework
|
||||
using the _Angular Testing Platform_.
|
||||
|
||||
|
||||
> [**测试**](guide/testing):使用 _Angular 测试平台_,在你的应用部件与 Angular 框架交互时进行单元测试。
|
||||
</div>
|
||||
|
@ -416,7 +416,7 @@ Here are the features which may require additional polyfills:
|
||||
|
||||
[JIT compilation](guide/aot-compiler).
|
||||
|
||||
[JIT 编译](guide/aot-compiler)
|
||||
[JIT 编译](guide/aot-compiler)。
|
||||
|
||||
Required to reflect for metadata.
|
||||
|
||||
|
@ -142,34 +142,34 @@ The following code snippets illustrate how the same kind of operation is defined
|
||||
|
||||
<table>
|
||||
|
||||
<th>
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Operation
|
||||
|
||||
操作
|
||||
|
||||
</td>
|
||||
</th>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Observable
|
||||
|
||||
可观察对象
|
||||
|
||||
</td>
|
||||
</th>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Promise
|
||||
|
||||
承诺
|
||||
|
||||
</td>
|
||||
|
||||
</th>
|
||||
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
@ -296,26 +296,26 @@ Here are some code samples that illustrate how the same kind of operation is def
|
||||
|
||||
<table>
|
||||
|
||||
<th>
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Observable
|
||||
|
||||
可观察对象
|
||||
|
||||
</td>
|
||||
</th>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Events API
|
||||
|
||||
事件 API
|
||||
|
||||
</td>
|
||||
|
||||
</th>
|
||||
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
@ -432,26 +432,26 @@ An observable produces values over time. An array is created as a static set of
|
||||
|
||||
<table>
|
||||
|
||||
<th>
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Observable
|
||||
|
||||
可观察对象
|
||||
|
||||
</td>
|
||||
</th>
|
||||
|
||||
<td>
|
||||
<th>
|
||||
|
||||
Array
|
||||
|
||||
数组
|
||||
|
||||
</td>
|
||||
|
||||
</th>
|
||||
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
|
73
aio/content/guide/custom-elements.md
Normal file
73
aio/content/guide/custom-elements.md
Normal file
@ -0,0 +1,73 @@
|
||||
# Elements
|
||||
|
||||
## Release Status
|
||||
|
||||
**Angular Labs Project** - experimental and unstable. **Breaking Changes Possible**
|
||||
|
||||
Targeted to land in the [6.x release cycle](https://github.com/angular/angular/blob/master/docs/RELEASE_SCHEDULE.md) of Angular - subject to change
|
||||
|
||||
## Overview
|
||||
|
||||
## 概览
|
||||
|
||||
Elements provides an API that allows developers to register Angular Components as Custom Elements
|
||||
("Web Components"), and bridges the built-in DOM API to Angular's component interface and change
|
||||
detection APIs.
|
||||
|
||||
```ts
|
||||
|
||||
//hello-world.ts
|
||||
import { Component, Input, NgModule } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'hello-world',
|
||||
template: `<h1>Hello {{name}}</h1>`
|
||||
})
|
||||
export class HelloWorld {
|
||||
@Input() name: string = 'World!';
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
declarations: [ HelloWorld ],
|
||||
entryComponents: [ HelloWorld ]
|
||||
})
|
||||
export class HelloWorldModule {}
|
||||
|
||||
```
|
||||
|
||||
```ts
|
||||
|
||||
//app.component.ts
|
||||
import { Component, NgModuleRef } from '@angular/core';
|
||||
import { createNgElementConstructor } from '@angular/elements';
|
||||
|
||||
import { HelloWorld } from './hello-world';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.css']
|
||||
})
|
||||
export class AppComponent {
|
||||
constructor(injector: Injector) {
|
||||
const NgElementConstructor = createNgElementConstructor(HelloWorld, {injector});
|
||||
customElements.register('hello-world', NgElementConstructor);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Once registered, these components can be used just like built-in HTML elements, because they *are*
|
||||
HTML Elements!
|
||||
|
||||
They can be used in any HTML page:
|
||||
|
||||
```html
|
||||
|
||||
<hello-world name="Angular"></hello-world>
|
||||
<hello-world name="Typescript"></hello-world>
|
||||
|
||||
```
|
||||
|
||||
Custom Elements are "self-bootstrapping" - they are automatically started when they are added to the
|
||||
DOM, and automatically destroyed when removed from the DOM.
|
@ -1039,11 +1039,7 @@ Callouts (like alerts) are meant to draw attention to important points. Use a ca
|
||||
|
||||
<div class="callout is-critical">
|
||||
|
||||
<header>
|
||||
|
||||
A critical point
|
||||
|
||||
</header>
|
||||
<header>A critical point</header>
|
||||
|
||||
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast, Schlitz crucifix
|
||||
|
||||
@ -1051,11 +1047,7 @@ A critical point
|
||||
|
||||
<div class="callout is-important">
|
||||
|
||||
<header>
|
||||
|
||||
An important point
|
||||
|
||||
</header>
|
||||
<header>An important point</header>
|
||||
|
||||
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast, Schlitz crucifix
|
||||
|
||||
@ -1063,11 +1055,7 @@ An important point
|
||||
|
||||
<div class="callout is-helpful">
|
||||
|
||||
<header>
|
||||
|
||||
A helpful point
|
||||
|
||||
</header>
|
||||
<header>A helpful point</header>
|
||||
|
||||
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast, Schlitz crucifix
|
||||
|
||||
@ -1079,11 +1067,7 @@ Here is the markup for the first of these callouts.
|
||||
|
||||
<div class="callout is-critical">
|
||||
|
||||
<header>
|
||||
|
||||
A critical point
|
||||
|
||||
</header>
|
||||
<header>A critical point</header>
|
||||
|
||||
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast, Schlitz crucifix
|
||||
|
||||
|
@ -153,12 +153,12 @@ The `loadComponent()` method chooses an ad using some math.
|
||||
|
||||
`loadComponent()` 方法使用某种算法选择了一个广告。
|
||||
|
||||
First, it sets the `currentAddIndex` by taking whatever it
|
||||
First, it sets the `currentAdIndex` by taking whatever it
|
||||
currently is plus one, dividing that by the length of the `AdItem` array, and
|
||||
using the _remainder_ as the new `currentAddIndex` value. Then, it uses that
|
||||
using the _remainder_ as the new `currentAdIndex` value. Then, it uses that
|
||||
value to select an `adItem` from the array.
|
||||
|
||||
(译注:循环选取算法)首先,它把 `currentAddIndex` 递增一,然后用它除以 `AdItem` 数组长度的*余数*作为新的 `currentAddIndex` 的值,
|
||||
(译注:循环选取算法)首先,它把 `currentAdIndex` 递增一,然后用它除以 `AdItem` 数组长度的*余数*作为新的 `currentAdIndex` 的值,
|
||||
最后用这个值来从数组中选取一个 `adItem`。
|
||||
|
||||
</div>
|
||||
|
@ -279,7 +279,7 @@ You may also be interested in the following:
|
||||
|
||||
* [Bootstrapping](guide/bootstrapping).
|
||||
|
||||
[引导](guide/bootstrapping).
|
||||
[引导启动](guide/bootstrapping)。
|
||||
|
||||
* [NgModules](guide/ngmodules).
|
||||
|
||||
|
@ -368,7 +368,6 @@ by the custom `id`:
|
||||
你可以在 `i18n` 属性的值中使用自定义 id 与描述信息的组合。
|
||||
下面的例子中,`i18n` 的属性中中就包含了一条跟在自定义 `id` 后面的描述信息:
|
||||
|
||||
|
||||
<code-example path='i18n/doc-files/app.component.html' region='i18n-attribute-id' title='app/app.component.html' linenums="false">
|
||||
|
||||
</code-example>
|
||||
|
@ -12,23 +12,23 @@ A basic understanding of the following:
|
||||
|
||||
* [Feature Modules](guide/feature-modules).
|
||||
|
||||
[特性模块](guide/feature-modules).
|
||||
[特性模块](guide/feature-modules)
|
||||
|
||||
* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|
||||
|
||||
[JavaScript 模块与 NgModules](guide/ngmodule-vs-jsmodule).
|
||||
[JavaScript 模块与 NgModules](guide/ngmodule-vs-jsmodule)。
|
||||
|
||||
* [Frequently Used Modules](guide/frequent-ngmodules).
|
||||
|
||||
[常用模块](guide/frequent-ngmodules).
|
||||
[常用模块](guide/frequent-ngmodules)。
|
||||
|
||||
* [Types of Feature Modules](guide/module-types).
|
||||
|
||||
[特性模块的分类](guide/module-types).
|
||||
[特性模块的分类](guide/module-types)。
|
||||
|
||||
* [Routing and Navigation](guide/router).
|
||||
|
||||
[路由与导航](guide/router).
|
||||
[路由与导航](guide/router)。
|
||||
|
||||
For the final sample app with two lazy loaded modules that this page describes, see the
|
||||
<live-example></live-example>.
|
||||
@ -355,7 +355,7 @@ CLI 还会把 `RouterModule.forChild(routes)` 添加到各个特性模块中。
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
你可能对下列内容感兴趣:
|
||||
你可能还会对下列内容感兴趣:
|
||||
|
||||
* [Routing and Navigation](guide/router).
|
||||
|
||||
@ -363,7 +363,7 @@ You may also be interested in the following:
|
||||
|
||||
* [Providers](guide/providers).
|
||||
|
||||
[服务提供商](guide/providers)。
|
||||
[提供商](guide/providers)。
|
||||
|
||||
* [Types of Feature Modules](guide/module-types).
|
||||
|
||||
|
@ -12,7 +12,7 @@ A basic understanding of the following concepts:
|
||||
|
||||
* [Feature Modules](guide/feature-modules).
|
||||
|
||||
[特性模块](guide/feature-modules)。
|
||||
[特性模块](guide/feature-modules)
|
||||
|
||||
* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|
||||
|
||||
@ -122,7 +122,7 @@ typical characteristics, in real world apps, you may see hybrids.
|
||||
|
||||
Routed
|
||||
|
||||
带路由的
|
||||
路由
|
||||
|
||||
</td>
|
||||
|
||||
@ -570,7 +570,7 @@ The following table summarizes the key characteristics of each feature module gr
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
你可能还对下列内容感兴趣:
|
||||
你可能还会对下列内容感兴趣:
|
||||
|
||||
* [Lazy Loading Modules with the Angular Router](guide/lazy-loading-ngmodules).
|
||||
|
||||
|
@ -10,7 +10,7 @@ A basic understanding of the following concepts:
|
||||
|
||||
* [Bootstrapping](guide/bootstrapping).
|
||||
|
||||
[引导](guide/bootstrapping)。
|
||||
[引导启动](guide/bootstrapping)。
|
||||
|
||||
* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|
||||
|
||||
@ -394,11 +394,11 @@ You may also be interested in the following:
|
||||
|
||||
* [Feature Modules](guide/feature-modules).
|
||||
|
||||
[特性模块](guide/feature-modules)。
|
||||
[特性模块](guide/feature-modules)
|
||||
|
||||
* [Entry Components](guide/entry-components).
|
||||
|
||||
[入口组件](guide/entry-components)。
|
||||
[入口组件](guide/entry-components)
|
||||
|
||||
* [Providers](guide/providers).
|
||||
|
||||
|
@ -12,6 +12,8 @@ A basic understanding of the following concepts:
|
||||
|
||||
* [NgModules](guide/ngmodules).
|
||||
|
||||
[Angular 模块](guide/ngmodules).
|
||||
|
||||
<hr />
|
||||
|
||||
NgModules help organize an application into cohesive blocks of functionality.
|
||||
|
@ -118,11 +118,11 @@ For more information on NgModules, see:
|
||||
|
||||
* [Bootstrapping](guide/bootstrapping).
|
||||
|
||||
[引导](guide/bootstrapping).
|
||||
[引导启动](guide/bootstrapping)。
|
||||
|
||||
* [Frequently used modules](guide/frequent-ngmodules).
|
||||
|
||||
[常用模块](guide/frequent-ngmodules).
|
||||
[常用模块](guide/frequent-ngmodules)。
|
||||
|
||||
* [Providers](guide/providers).
|
||||
|
||||
|
@ -149,7 +149,7 @@ You may also be interested in the following:
|
||||
|
||||
* [Providers](guide/providers).
|
||||
|
||||
[服务提供商](guide/providers).
|
||||
[提供商](guide/providers)。
|
||||
|
||||
* [Types of NgModules](guide/module-types).
|
||||
|
||||
|
@ -80,7 +80,7 @@ The *devDependencies* are only necessary to *develop* the application.
|
||||
|
||||
{@a dependencies}
|
||||
|
||||
## *dependencies*
|
||||
## *Dependencies*
|
||||
|
||||
The `dependencies` section of `package.json` contains:
|
||||
|
||||
@ -193,7 +193,7 @@ which polyfills missing features for several popular browser.
|
||||
|
||||
{@a dev-dependencies}
|
||||
|
||||
## *devDependencies*
|
||||
## *DevDependencies*
|
||||
|
||||
The packages listed in the *devDependencies* section of the `package.json` help you develop the application on your local machine.
|
||||
|
||||
|
@ -140,7 +140,7 @@ You may also be interested in:
|
||||
|
||||
* [Lazy Loading Modules](guide/lazy-loading-ngmodules).
|
||||
|
||||
[惰性加载模块](guide/lazy-loading-ngmodules)。
|
||||
[惰性加载模块](guide/lazy-loading-ngmodules).
|
||||
|
||||
* [NgModule FAQ](guide/ngmodule-faq).
|
||||
|
||||
|
@ -137,9 +137,9 @@ You can find it in `./src/app/app.component.ts`.
|
||||
它就是名叫 `app-root` 的*根组件*。
|
||||
你可以在 `./src/app/app.component.ts` 目录下找到它。
|
||||
|
||||
Open the component file and change the `title` property from _Welcome to app!!_ to _Welcome to My First Angular App!!_:
|
||||
Open the component file and change the `title` property from `'app'` to `'My First Angular App!'`.
|
||||
|
||||
打开这个组件文件,并且把 `title` 属性从 _Welcome to app!!_ 改为 _Welcome to My First Angular App!!_ :
|
||||
打开这个组件文件,并且把 `title` 属性从 `'app'` 改为 `'My First Angular App!'`:
|
||||
|
||||
<code-example path="cli-quickstart/src/app/app.component.ts" region="title" title="src/app/app.component.ts" linenums="false"></code-example>
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -97,7 +97,7 @@ Doing this could break lazy-loading into currently running apps, especially if t
|
||||
|
||||
## More on Angular service workers
|
||||
|
||||
## 关于 Angular Service Worker 的更多知识
|
||||
## 关于 Angular Service Worker 的更多信息
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
|
@ -553,7 +553,7 @@ the past on your site.
|
||||
|
||||
## More on Angular service workers
|
||||
|
||||
## 关于 Angular Service Worker 的更多知识
|
||||
## 关于 Angular Service Worker 的更多信息
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
|
@ -363,7 +363,7 @@ Service Worker *在后台*安装好了这个更新后的版本,下次加载或
|
||||
|
||||
## More on Angular service workers
|
||||
|
||||
## 关于 Angular Service Worker 的更多知识
|
||||
## 关于 Angular Service Worker 的更多信息
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
|
@ -122,7 +122,7 @@ The remainder of this Angular documentation specifically addresses the Angular i
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
你可能还对下列内容感兴趣:
|
||||
你可能还会对下列内容感兴趣:
|
||||
|
||||
* [Getting Started with service workers](guide/service-worker-getting-started).
|
||||
|
||||
|
@ -12,7 +12,7 @@ A basic understanding of the following:
|
||||
|
||||
* [Feature Modules](guide/feature-modules).
|
||||
|
||||
[特性模块](guide/feature-modules).
|
||||
[特性模块](guide/feature-modules)
|
||||
|
||||
* [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule).
|
||||
|
||||
@ -20,11 +20,11 @@ A basic understanding of the following:
|
||||
|
||||
* [Frequently Used Modules](guide/frequent-ngmodules).
|
||||
|
||||
[常用模块](guide/frequent-ngmodules).
|
||||
[常用模块](guide/frequent-ngmodules)。
|
||||
|
||||
* [Routing and Navigation](guide/router).
|
||||
|
||||
[路由与导航](guide/router).
|
||||
[路由与导航](guide/router)。
|
||||
|
||||
* [Lazy loading modules](guide/lazy-loading-ngmodules).
|
||||
|
||||
@ -123,11 +123,11 @@ To read about sharing services, see [Providers](guide/providers).
|
||||
|
||||
You may also be interested in the following:
|
||||
|
||||
你可能还对下列内容感兴趣:
|
||||
你可能还会对下列内容感兴趣:
|
||||
|
||||
* [Providers](guide/providers).
|
||||
|
||||
[服务提供商](guide/providers).
|
||||
[提供商](guide/providers)。
|
||||
|
||||
* [Types of Feature Modules](guide/module-types).
|
||||
|
||||
|
@ -298,7 +298,7 @@ You may also be interested in:
|
||||
|
||||
* [Lazy Loading Modules](guide/lazy-loading-ngmodules).
|
||||
|
||||
[惰性加载模块](guide/lazy-loading-ngmodules)。
|
||||
[惰性加载模块](guide/lazy-loading-ngmodules).
|
||||
|
||||
* [NgModule FAQ](guide/ngmodule-faq).
|
||||
|
||||
|
@ -1243,6 +1243,38 @@ There is no harm in calling `detectChanges()` more often than is strictly necess
|
||||
|
||||
<hr>
|
||||
|
||||
{@a dispatch-event}
|
||||
|
||||
#### Change an input value with _dispatchEvent()_
|
||||
|
||||
#### 使用 `dispatchEvent()` 修改输入值
|
||||
|
||||
To simulate user input, you can find the input element and set its `value` property.
|
||||
|
||||
要想模拟用户输入,你就要找到 `<input>` 元素并设置它的 `value` 属性。
|
||||
|
||||
You will call `fixture.detectChanges()` to trigger Angular's change detection.
|
||||
But there is an essential, intermediate step.
|
||||
|
||||
你要调用 `fixture.detectChanges()` 来触发 Angular 的变更检测。
|
||||
但那只是一个基本的中间步骤。
|
||||
|
||||
Angular doesn't know that you set the input element's `value` property.
|
||||
It won't read that property until you raise the element's `input` event by calling `dispatchEvent()`.
|
||||
_Then_ you call `detectChanges()`.
|
||||
|
||||
Angular 不知道你设置了这个 `<input>` 元素的 `value` 属性。
|
||||
在你通过调用 `dispatchEvent()` 触发了该输入框的 `input` 事件之前,它不能读到那个值。
|
||||
*调用完之后*你再调用 `detectChanges()`。
|
||||
|
||||
The following example demonstrates the proper sequence.
|
||||
|
||||
下面的例子演示了这个调用顺序。
|
||||
|
||||
<code-example path="testing/src/app/hero/hero-detail.component.spec.ts" region="title-case-pipe" title="app/hero/hero-detail.component.spec.ts (pipe test)"></code-example>
|
||||
|
||||
<hr>
|
||||
|
||||
### Component with external files
|
||||
|
||||
### 带有外部文件的组件
|
||||
|
@ -386,16 +386,16 @@ You can get runtime information about the current platform and the `appId` by in
|
||||
|
||||
#### 在 HTTP 中使用绝对地址
|
||||
|
||||
The tutorial's `HeroService` and `HeroSearchService` delegate to the Angular `Http` module to fetch application data.
|
||||
The tutorial's `HeroService` and `HeroSearchService` delegate to the Angular `HttpClient` module to fetch application data.
|
||||
These services send requests to _relative_ URLs such as `api/heroes`.
|
||||
|
||||
教程中的 `HeroService` 和 `HeroSearchService` 都委托了 Angular 的 `Http` 模块来获取应用数据。
|
||||
教程中的 `HeroService` 和 `HeroSearchService` 都委托了 Angular 的 `HttpClient` 模块来获取应用数据。
|
||||
那些服务都把请求发送到了*相对* URL,比如 `api/heroes`。
|
||||
|
||||
In a Universal app, `Http` URLs must be _absolute_ (e.g., `https://my-server.com/api/heroes`)
|
||||
In a Universal app, HTTP URLs must be _absolute_, for example, `https://my-server.com/api/heroes`
|
||||
even when the Universal web server is capable of handling those requests.
|
||||
|
||||
在 Universal 应用中,`Http` 的 URL 必须是*绝对地址*(比如 `https://my-server.com/api/heroes`),
|
||||
在 Universal 应用中,HTTP 的 URL 必须是*绝对地址*(比如 `https://my-server.com/api/heroes`),
|
||||
只有这样,Universal 的 Web 服务器才能处理那些请求。
|
||||
|
||||
You'll have to change the services to make requests with absolute URLs when running on the server
|
||||
@ -741,7 +741,7 @@ This config extends from the root's `tsconfig.json` file. Certain settings are n
|
||||
|
||||
这个配置扩展了根目录下的 `tsconfig.json` 文件,注意它们在某些设置上的差异。
|
||||
|
||||
* The `module` property must be **commonjs** which can be require()'d into our server application.
|
||||
* The `module` property must be **commonjs** which can be required into our server application.
|
||||
|
||||
`module` 属性必须是 **commonjs**,这样它才能被 `require()` 进你的服务端应用。
|
||||
|
||||
|
@ -423,11 +423,9 @@ Polyfills 最好跟应用代码和 vendor 代码区分开来单独打包。在 `
|
||||
|
||||
<div class="callout is-critical">
|
||||
|
||||
<header>
|
||||
<header>Loading polyfills</header>
|
||||
|
||||
Loading polyfills
|
||||
|
||||
</header>
|
||||
<header>加载腻子脚本</header>
|
||||
|
||||
Load `zone.js` early within `polyfills.ts`, immediately after the other ES6 and metadata shims.
|
||||
|
||||
|
@ -414,7 +414,7 @@ The finished `<li>` looks like this:
|
||||
|
||||
Your app should look like this <live-example></live-example>.
|
||||
|
||||
本应用现在变成了这样:<live-example></live-example>。
|
||||
你的应用现在变成了这样:<live-example></live-example>。
|
||||
|
||||
Here are the code files discussed on this page, including the `HeroesComponent` styles.
|
||||
|
||||
|
@ -32,9 +32,9 @@ When you’re done, users will be able to navigate the app like this:
|
||||
|
||||
</figure>
|
||||
|
||||
## Add the _AppRoutingModule_
|
||||
## Add the `AppRoutingModule`
|
||||
|
||||
## 添加 _AppRoutingModule_
|
||||
## 添加 `AppRoutingModule`
|
||||
|
||||
An Angular best practice is to load and configure the router in a separate, top-level module
|
||||
that is dedicated to routing and imported by the root `AppModule`.
|
||||
@ -235,9 +235,9 @@ You should see the familiar heroes master/detail view.
|
||||
|
||||
{@a routerlink}
|
||||
|
||||
## Add a navigation link (_routerLink_)
|
||||
## Add a navigation link (`routerLink`)
|
||||
|
||||
## 添加路由链接 (*routerLink*)
|
||||
## 添加路由链接 (`routerLink`)
|
||||
|
||||
Users shouldn't have to paste a route URL into the address bar.
|
||||
They should be able to click a link to navigate.
|
||||
@ -485,7 +485,7 @@ and liberate it from the `HeroesComponent`.
|
||||
|
||||
在这一节,你将能导航到 `HeroDetailComponent`,并把它从 `HeroesComponent` 中解放出来。
|
||||
|
||||
### Delete _hero details_ from _HeroesComponent_
|
||||
### Delete _hero details_ from `HeroesComponent`
|
||||
|
||||
### 从 `HeroesComponent` 中删除*英雄详情*
|
||||
|
||||
@ -551,7 +551,7 @@ At this point, all application routes are in place.
|
||||
|
||||
</code-example>
|
||||
|
||||
### _DashboardComponent_ hero links
|
||||
### `DashboardComponent` hero links
|
||||
|
||||
### `DashboardComponent` 中的英雄链接
|
||||
|
||||
@ -580,7 +580,7 @@ to insert the current interation's `hero.id` into each
|
||||
|
||||
{@a heroes-component-links}
|
||||
|
||||
### _HeroesComponent_ hero links
|
||||
### `HeroesComponent` hero links
|
||||
|
||||
### `HeroesComponent` 中的英雄链接
|
||||
|
||||
@ -752,9 +752,9 @@ Add it now.
|
||||
刷新浏览器,应用挂了。出现一个编译错误,因为 `HeroService` 没有一个名叫 `getHero()` 的方法。
|
||||
这就添加它。
|
||||
|
||||
### Add *HeroService.getHero()*
|
||||
### Add `HeroService.getHero()`
|
||||
|
||||
### 添加 *HeroService.getHero()*
|
||||
### 添加 `HeroService.getHero()`
|
||||
|
||||
Open `HeroService` and add this `getHero()` method
|
||||
|
||||
@ -865,9 +865,9 @@ Here are the code files discussed on this page and your app should look like thi
|
||||
|
||||
{@a appmodule}
|
||||
|
||||
#### _AppRoutingModule_ and _AppModule_
|
||||
#### _AppRoutingModule_, _AppModule_, and _HeroService_
|
||||
|
||||
#### _AppRoutingModule_ 与 _AppModule_
|
||||
#### `AppRoutingModule`、`AppModule` 和 `HeroService`
|
||||
|
||||
<code-tabs>
|
||||
|
||||
@ -879,6 +879,10 @@ Here are the code files discussed on this page and your app should look like thi
|
||||
title="src/app/app.module.ts"
|
||||
path="toh-pt5/src/app/app.module.ts">
|
||||
</code-pane>
|
||||
<code-pane
|
||||
title="src/app/hero.service.ts"
|
||||
path="toh-pt5/src/app/hero.service.ts">
|
||||
</code-pane>
|
||||
|
||||
</code-tabs>
|
||||
|
||||
|
@ -15911,7 +15911,7 @@
|
||||
},
|
||||
{
|
||||
"original": "Routed",
|
||||
"translation": "带路由的",
|
||||
"translation": "路由",
|
||||
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/module-types.md"
|
||||
},
|
||||
{
|
||||
@ -17641,7 +17641,7 @@
|
||||
},
|
||||
{
|
||||
"original": "You may also be interested in the following:",
|
||||
"translation": "你可能还会对下列内容感兴趣:",
|
||||
"translation": "你可能还对下列内容感兴趣:",
|
||||
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/ngmodules.md"
|
||||
},
|
||||
{
|
||||
@ -19269,11 +19269,6 @@
|
||||
"translation": "文件",
|
||||
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/quickstart.md"
|
||||
},
|
||||
{
|
||||
"original": "Purpose",
|
||||
"translation": "目的",
|
||||
"sourceFile": "/Users/twer/private/GDE/angular-cn/aio/content/guide/quickstart.md"
|
||||
},
|
||||
{
|
||||
"original": "Inside `e2e/` live the end-to-end tests.\n They shouldn't be inside `src/` because e2e tests are really a separate app that\n just so happens to test your main app.\n That's also why they have their own `tsconfig.e2e.json`.",
|
||||
"translation": "在 `e2e/` 下是端到端(end-to-end)测试。\n 它们不在 `src/` 下,是因为端到端测试实际上和应用是相互独立的,它只适用于测试你的应用而已。\n 这也就是为什么它会拥有自己的 `tsconfig.json`。",
|
||||
|
Loading…
x
Reference in New Issue
Block a user