从 AngularJS 到 Angular 快速参考

Angular is the name for the Angular of today and tomorrow. AngularJS is the name for all v1.x versions of Angular.

This guide helps you transition from AngularJS to Angular by mapping AngularJS syntax to the equivalent Angular syntax.

在AngularJS和Angular之间,有很多不同的概念和语法。 本章提供了一个快速的参考指南,指出一些常用的AngularJS语法及其在Angular中的等价物。

See the Angular syntax in this .

可到中查看Angular语法

Contents

内容

Template basics

模板基础

Templates are the user-facing part of an Angular application and are written in HTML. The following table lists some of the key AngularJS template features with their equivalent Angular template syntax.

模板是Angular应用中的门面部分,它是用HTML写的。下表中是一些AngularJS中的关键模板特性及其在Angular中的等价语法。

AngularJSAngular

Bindings/interpolation

绑定/插值表达式

Your favorite hero is: {{vm.favoriteHero}}

In AngularJS, an expression in curly braces denotes one-way binding. This binds the value of the element to a property in the controller associated with this template.

在AngularJS中,花括号中的表达式代表单向绑定。 它把元素的值绑定到了与模板相关控制器的属性上。

When using the controller as syntax, the binding is prefixed with the controller alias (vm or $ctrl) because you have to be specific about the source of the binding.

当使用controller as语法时,该绑定需要用控制器的别名(vm)为前缀,这是因为我们不得不通过它来指定绑定源。

Bindings/interpolation

绑定/插值表达式

Your favorite hero is: {{favoriteHero}}

In Angular, a template expression in curly braces still denotes one-way binding. This binds the value of the element to a property of the component. The context of the binding is implied and is always the associated component, so it needs no reference variable.

在Angular中,花括号中的模板表达式同样代表单向绑定。 它把元素的值绑定到了组件的属性上。 它绑定的上下文变量是隐式的,并且总是关联到组件。 所以,它不需要一个引用变量。

For more information, see the Interpolation section of the Template Syntax page.

要了解更多,请参见模板语法中的插值表达式部分。

Filters

过滤器

<td>{{movie.title | uppercase}}</td>

To filter output in AngularJS templates, use the pipe character (|) and one or more filters.

要在AngularJS中过滤输出,使用管道字符(|)以及一个或多个过滤器。

This example filters the title property to uppercase.

在这个例子中,我们把title属性过滤成了大写形式。

Pipes

管道

<td>{{movie.title | uppercase}}</td>

In Angular you use similar syntax with the pipe (|) character to filter output, but now you call them pipes. Many (but not all) of the built-in filters from AngularJS are built-in pipes in Angular.

在Angular中,我们使用相似的语法 —— 用管道字符(|)来过滤输出,但是现在直接把它叫做管道了。 很多(但不是所有)AngularJS中的内置过滤器也成了Angular中的内置管道。

For more information, see Filters/pipes below.

请参见下面过滤器/管道了解更多信息。

Local variables

局部变量

<tr ng-repeat="movie in vm.movies"> <td>{{movie.title}}</td> </tr>

Here, movie is a user-defined local variable.

这里的movie是一个用户定义的局部变量

Input variables

输入变量

<tr *ngFor="let movie of movies"> <td>{{movie.title}}</td> </tr>

Angular has true template input variables that are explicitly defined using the let keyword.

在Angular中,我们有了真正的模板输入变量,它需要使用let关键字进行明确定义。

For more information, see the ngFor micro-syntax section of the Template Syntax page.

要了解更多信息,请参见模板语法中的ngFor微语法部分。

Back to top

返回顶部

Template directives

模板指令

AngularJS provides more than seventy built-in directives for templates. Many of them aren't needed in Angular because of its more capable and expressive binding system. The following are some of the key AngularJS built-in directives and their equivalents in Angular.

AngularJS 为模板提供了七十多个内置指令。 在 Angular 中,它们很多都已经不需要了,因为 Angular 有了一个更加强大、快捷的绑定系统。 下面是一些AngularJS 中的关键指令及其在 Angular 中的等价物。

AngularJSAngular

ng-app

<body ng-app="movieHunter">

The application startup process is called bootstrapping.

应用的启动过程被称为引导

Although you can bootstrap an AngularJS app in code, many applications bootstrap declaratively with the ng-app directive, giving it the name of the application's module (movieHunter).

虽然可以从代码中引导Angular应用, 但很多应用都是通过ng-app指令进行声明式引导的,只要给它一个应用模块的名字(movieHunter)就可以了。

Bootstrapping

引导

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }

Angular doesn't have a bootstrap directive. To launch the app in code, explicitly bootstrap the application's root module (AppModule) in main.ts and the application's root component (AppComponent) in app.module.ts.

Angular 没有引导指令。 我们总是通过显式调用一个bootstrap函数,并传入应用模块的名字(AppComponent)来启动应用。

For more information see the Setup page.

要了解更多,参见搭建本地开发环境

ng-class

<div ng-class="{active: isActive}"> <div ng-class="{active: isActive, shazam: isImportant}">

In AngularJS, the ng-class directive includes/excludes CSS classes based on an expression. That expression is often a key-value control object with each key of the object defined as a CSS class name, and each value defined as a template expression that evaluates to a Boolean value.

在AngularJS中,ng-class指令会基于一个表达式来包含/排除某些CSS类。该表达式通常是一个“键-值”型的控制对象, 对象中的每一个键代表一个CSS类名,每一个值定义为一个返回布尔值的模板表达式。

In the first example, the active class is applied to the element if isActive is true.

在第一个例子中,当isActive为真时,active类会被应用到元素上。

You can specify multiple classes, as shown in the second example.

就像第二个例子中展示的,可以指定多个CSS类。

ngClass

<div [ngClass]="{active: isActive}"> <div [ngClass]="{active: isActive, shazam: isImportant}"> <div [class.active]="isActive">

In Angular, the ngClass directive works similarly. It includes/excludes CSS classes based on an expression.

在Angular中,ngClass指令用类似的方式工作。 它根据一个表达式包含/排除某些CSS类。

In the first example, the active class is applied to the element if isActive is true.

在第一个例子中,如果isActive为真,则active类被应用到那个元素上。

You can specify multiple classes, as shown in the second example.

就像第二个例子中所展示的那样,可以同时指定多个类。

Angular also has class binding, which is a good way to add or remove a single class, as shown in the third example.

Angular还有类绑定,它是单独添加或移除一个类的好办法 —— 就像第三个例子中展示的。

For more information see the Attribute, class, and style bindings section of the Template Syntax page.

要了解更多信息,参见模板语法中的属性、CSS类和样式绑定部分。

ng-click

<button ng-click="vm.toggleImage()"> <button ng-click="vm.toggleImage($event)">

In AngularJS, the ng-click directive allows you to specify custom behavior when an element is clicked.

在AngularJS中,ng-click指令指定当元素被点击时的自定义行为。

In the first example, when the user clicks the button, the toggleImage() method in the controller referenced by the vm controller as alias is executed.

在第一个例子中,如果用户点击了这个按钮,那么控制器的toggleImage()方法就会被执行,这个控制器是被controller as中指定的vm别名所引用的。

The second example demonstrates passing in the $event object, which provides details about the event to the controller.

第二个例子演示了传入$event对象,它提供了事件的详情,并被传到控制器。

Bind to the click event

绑定到click事件

<button (click)="toggleImage()"> <button (click)="toggleImage($event)">

AngularJS event-based directives do not exist in Angular. Rather, define one-way binding from the template view to the component using event binding.

AngularJS基于事件的指令在Angular中已经不存在了。 不过,可以使用事件绑定来定义从模板视图到组件的单向数据绑定。

For event binding, define the name of the target event within parenthesis and specify a template statement, in quotes, to the right of the equals. Angular then sets up an event handler for the target event. When the event is raised, the handler executes the template statement.

要使用事件绑定,把目标事件的名字放在圆括号中,并且使用等号右侧引号中的模板语句对它赋值。 然后Angular为这个目标时间设置事件处理器。当事件被触发时,这个处理器就会执行模板语句。

In the first example, when a user clicks the button, the toggleImage() method in the associated component is executed.

在第一个例子中,当用户点击此按钮时,相关组件中的toggleImage()方法就被执行了。

The second example demonstrates passing in the $event object, which provides details about the event to the component.

第二个例子演示了如何传入$event对象,它为组件提供了此事件的详情。

For a list of DOM events, see: https://developer.mozilla.org/en-US/docs/Web/Events.

要查看DOM事件的列表,请参见网络事件

For more information, see the Event binding section of the Template Syntax page.

要了解更多,请参见模板语法中的事件绑定部分。

ng-controller

<div ng-controller="MovieListCtrl as vm">

In AngularJS, the ng-controller directive attaches a controller to the view. Using the ng-controller (or defining the controller as part of the routing) ties the view to the controller code associated with that view.

在AngularJS中,ng-controller指令把控制器附加到视图上。 使用ng-controller(或把控制器定义为路由的一部分)把视图及其控制器的代码联系在一起。

Component decorator

Component装饰器

@Component({ selector: 'movie-list', templateUrl: './movie-list.component.html', styleUrls: [ './movie-list.component.css' ], })

In Angular, the template no longer specifies its associated controller. Rather, the component specifies its associated template as part of the component class decorator.

在Angular中,模板不用再指定它相关的控制器。 反过来,组件会在组件类的装饰器中指定与它相关的模板。

For more information, see Architecture Overview.

要了解更多,请参见架构概览

ng-hide

In AngularJS, the ng-hide directive shows or hides the associated HTML element based on an expression. For more information, see ng-show.

在AngularJS中,ng-hide指令会基于一个表达式显示或隐藏相关的HTML元素。 参见ng-show了解更多。

Bind to the hidden property

绑定hidden属性

In Angular, you use property binding; there is no built-in hide directive. For more information, see ng-show.

在Angular中,并没有一个内置的hide指令,可以改用属性绑定。 参见ng-show了解更多。

ng-href

<a ng-href="angularDocsUrl">Angular Docs</a>

The ng-href directive allows AngularJS to preprocess the href property so that it can replace the binding expression with the appropriate URL before the browser fetches from that URL.

ng-href指令允许AngularJS对href属性进行预处理,以便它能在浏览器获取那个URL之前,使用一个返回适当URL的绑定表达式替换它。

In AngularJS, the ng-href is often used to activate a route as part of navigation.

在AngularJS 中,ng-href通常用来作为导航的一部分,激活一个路由。

<a ng-href="#movies">Movies</a>

Routing is handled differently in Angular.

路由在Angular中的处理方式不同。

Bind to the href property

绑定到href属性

<a [href]="angularDocsUrl">Angular Docs</a>

Angular uses property binding; there is no built-in href directive. Place the element's href property in square brackets and set it to a quoted template expression.

在Angular中,并没有内置的href指令,改用属性绑定。 我们把元素的href属性放在方括号中,并把它设成一个引号中的模板表达式。

For more information see the Property binding section of the Template Syntax page.

要了解属性绑定的更多知识,参见模板语法

In Angular, href is no longer used for routing. Routing uses routerLink, as shown in the following example.

在Angular中,href不再用作路由,而是改用第三个例子中所展示的routerLink指令。

<a [routerLink]="['/movies']">Movies</a>

For more information on routing, see the RouterLink binding section of the Routing & Navigation page.

要了解关于路由的更多信息,请参见路由与导航RouterLink绑定部分。

ng-if

<table ng-if="movies.length">

In AngularJS, the ng-if directive removes or recreates a portion of the DOM, based on an expression. If the expression is false, the element is removed from the DOM.

在AngularJS中,ng-if指令会根据一个表达式来移除或重建DOM中的一部分。如果表达式为假,元素就会被从DOM中移除。

In this example, the <table> element is removed from the DOM unless the movies array has a length greater than zero.

在这个例子中,除非movies数组的长度大于0,否则<table>元素就会被从DOM中移除。

*ngIf

<table *ngIf="movies.length">

The *ngIf directive in Angular works the same as the ng-if directive in AngularJS. It removes or recreates a portion of the DOM based on an expression.

Angular中的*ngIf指令与AngularJS中的ng-if指令一样, 它根据表达式的值移除或重建DOM中的一部分。

In this example, the <table> element is removed from the DOM unless the movies array has a length.

在这个例子中,除非movies数组的长度大于0,否则<table>元素就会被从DOM中移除。

The (*) before ngIf is required in this example. For more information, see Structural Directives.

在这个例子中ngIf前的星号(*)是必须的。 要了解更多信息,参见结构型指令

ng-model

<input ng-model="vm.favoriteHero"/>

In AngularJS, the ng-model directive binds a form control to a property in the controller associated with the template. This provides two-way binding, whereby any change made to the value in the view is synchronized with the model, and any change to the model is synchronized with the value in the view.

在Angular1中,ng-model指令把一个表单控件绑定到了模板相关控制器的一个属性上。 这提供了双向绑定功能,因此,任何对视图中值的改动,都会同步到模型中,对模型的改动,也会同步到视图中。

ngModel

<input [(ngModel)]="favoriteHero" />

In Angular, two-way binding is denoted by [()], descriptively referred to as a "banana in a box". This syntax is a shortcut for defining both property binding (from the component to the view) and event binding (from the view to the component), thereby providing two-way binding.

在Angular中,双向绑定使用[()]标记出来,它被形象的比作“盒子中的香蕉”。 这种语法是一个简写形式,用来同时定义一个属性绑定(从组件到视图)和一个事件绑定(从视图到组件),因此,我们得到了双向绑定。

For more information on two-way binding with ngModel, see the NgModel—Two-way binding to form elements with [(ngModel)] section of the Template Syntax page.

要了解使用ngModel进行双向绑定的更多知识,参见模板语法中的NgModel—使用[(ngModel)]进行双向绑定部分。

ng-repeat

<tr ng-repeat="movie in vm.movies">

In AngularJS, the ng-repeat directive repeats the associated DOM element for each item in the specified collection.

在Angular1中,ng-repeat指令会为指定集合中的每一个条目重复渲染相关的DOM元素。

In this example, the table row (<tr>) element repeats for each movie object in the collection of movies.

在这个例子中,对movies集合中的每一个movie对象重复渲染了这个表格行元素(<tr>)。

*ngFor

<tr *ngFor="let movie of movies">

The *ngFor directive in Angular is similar to the ng-repeat directive in AngularJS. It repeats the associated DOM element for each item in the specified collection. More accurately, it turns the defined element (<tr> in this example) and its contents into a template and uses that template to instantiate a view for each item in the list.

Angular中的*ngFor指令类似于AngularJS中的ng-repeat指令。 它为指定集合中的每一个条目重复渲染了相关的DOM元素。 更准确的说,它把被界定出来的元素(这个例子中是<tr>)及其内容转成了一个模板,并使用那个模板来为列表中的每一个条目实例化一个视图。

Notice the other syntax differences: The (*) before ngFor is required; the let keyword identifies movie as an input variable; the list preposition is of, not in.

请注意其它语法上的差异: 在ngFor前面的星号(*)是必须的;let关键字把movie标记成一个输入变量;列表中使用的介词是of,而不再是in

For more information, see Structural Directives.

要了解更多信息,参见结构性指令

ng-show

<h3 ng-show="vm.favoriteHero"> Your favorite hero is: {{vm.favoriteHero}} </h3>

In AngularJS, the ng-show directive shows or hides the associated DOM element, based on an expression.

在AngularJS中,ng-show指令根据一个表达式来显示或隐藏相关的DOM元素。

In this example, the <div> element is shown if the favoriteHero variable is truthy.

在这个例子中,如果favoriteHero变量为真,<div>元素就会显示出来。

Bind to the hidden property

绑定到hidden属性

<h3 [hidden]="!favoriteHero"> Your favorite hero is: {{favoriteHero}} </h3>

Angular uses property binding; there is no built-in show directive. For hiding and showing elements, bind to the HTML hidden property.

在Angular中,并没有内置的show指令,可以改用属性绑定。 要隐藏或显示一个元素,绑定到它的hidden属性就可以了。

To conditionally display an element, place the element's hidden property in square brackets and set it to a quoted template expression that evaluates to the opposite of show.

要想有条件的显示一个元素,就把该元素的hidden属性放到一个方括号里,并且把它设置为引号中的模板表达式,它的结果应该是与显示相反的值。

In this example, the <div> element is hidden if the favoriteHero variable is not truthy.

在这个例子中,如果favoriteHero变量不是真值,<div>元素就会被隐藏。

For more information on property binding, see the Property binding
section of the Template Syntax page.

要了解关于属性绑定的更多信息,参见模板语法中的模板表达式部分。

ng-src

<img ng-src="{{movie.imageurl}}">

The ng-src directive allows AngularJS to preprocess the src property so that it can replace the binding expression with the appropriate URL before the browser fetches from that URL.

ng-src指令允许AngularJS对src属性进行预处理,以便它能够在浏览器获取此URL之前,用一个返回适当URL的绑定表达式替换它。

Bind to the src property

绑定到src属性

<img [src]="movie.imageurl">

Angular uses property binding; there is no built-in src directive. Place the src property in square brackets and set it to a quoted template expression.

在Angular中,并没有一个内置的src指令,可以使用属性绑定。 把src属性放到方括号中,并且把它设为一个引号中的绑定表达式。

For more information on property binding, see the Property binding
section of the Template Syntax page.

要了解属性绑定的更多知识,参见模板语法中的属性绑定部分。

ng-style

<div ng-style="{color: colorPreference}">

In AngularJS, the ng-style directive sets a CSS style on an HTML element based on an expression. That expression is often a key-value control object with each key of the object defined as a CSS property, and each value defined as an expression that evaluates to a value appropriate for the style.

在AngularJS中,ng-style指令根据一个绑定表达式设置一个HTML元素的CSS样式。 该表达式通常是一个“键-值”形式的控制对象,对象的每个键都是一个CSS属性,每个值都是一个能计算为此样式的合适值的表达式。

In the example, the color style is set to the current value of the colorPreference variable.

在这个例子中,color样式被设置为colorPreference变量的当前值。

ngStyle

<div [ngStyle]="{color: colorPreference}"> <div [style.color]="colorPreference">

In Angular, the ngStyle directive works similarly. It sets a CSS style on an HTML element based on an expression.

在Angular中,ngStyle指令的工作方式与此类似。它根据一个表达式设置HTML元素上的CSS样式。

In the first example, the color style is set to the current value of the colorPreference variable.

在第一个例子中,color样式被设置成了colorPreference变量的当前值。

Angular also has style binding, which is good way to set a single style. This is shown in the second example.

Angular还有样式绑定语法,它是单独设置一个样式的好方法。它展示在第二个例子中。

For more information on style binding, see the Style binding section of the Template Syntax page.

要了解样式绑定的更多知识,参见模板语法中的样式绑定部分。

For more information on the ngStyle directive, see NgStyle section of the Template Syntax page.

要了解关于ngStyle指令的更多知识,参见模板语法中的NgStyle部分。

ng-switch

<div ng-switch="vm.favoriteHero && vm.checkMovieHero(vm.favoriteHero)"> <div ng-switch-when="true"> Excellent choice! </div> <div ng-switch-when="false"> No movie, sorry! </div> <div ng-switch-default> Please enter your favorite hero. </div> </div>

In AngularJS, the ng-switch directive swaps the contents of an element by selecting one of the templates based on the current value of an expression.

在Angular1中,ng-switch指令根据一个表达式的当前值把元素的内容替换成几个模板之一。

In this example, if favoriteHero is not set, the template displays "Please enter ...". If favoriteHero is set, it checks the movie hero by calling a controller method. If that method returns true, the template displays "Excellent choice!". If that methods returns false, the template displays "No movie, sorry!".

在这个例子中,如果favoriteHero没有设置,则模板显示“Please enter ...”。 如果favoriteHero设置过,它就会通过调用一个控制其方法来检查它是否电影里的英雄。 如果该方法返回true,模板就会显示“Excellent choice!”。 如果该方法返回false,该模板就会显示“No movie, sorry!”。

ngSwitch

<span [ngSwitch]="favoriteHero && checkMovieHero(favoriteHero)"> <p *ngSwitchCase="true"> Excellent choice! </p> <p *ngSwitchCase="false"> No movie, sorry! </p> <p *ngSwitchDefault> Please enter your favorite hero. </p> </span>

In Angular, the ngSwitch directive works similarly. It displays an element whose *ngSwitchCase matches the current ngSwitch expression value.

在Angular中,ngSwitch指令的工作方式与此类似。 它会显示那个与ngSwitch表达式的当前值匹配的那个*ngSwitchCase所在的元素。

In this example, if favoriteHero is not set, the ngSwitch value is null and *ngSwitchDefault displays, "Please enter ...". If favoriteHero is set, the app checks the movie hero by calling a component method. If that method returns true, the app selects *ngSwitchCase="true" and displays: "Excellent choice!" If that methods returns false, the app selects *ngSwitchCase="false" and displays: "No movie, sorry!"

在这个例子中,如果favoriteHero没有设置,则ngSwitch的值是null,我们会看到 *ngSwitchDefault中的段落“Please enter ...”。 如果favoriteHero被设置了,它就会通过调用一个组件方法来检查电影英雄。 如果该方法返回true,我们就会看到“Excellent choice!”。 如果该方法返回false,我们就会看到“No movie, sorry!”。

The (*) before ngSwitchCase and ngSwitchDefault is required in this example.

在这个例子中,ngSwitchCasengSwitchDefault前面的星号(*)是必须的。

For more information, see The NgSwitch directives section of the Template Syntax page.

要了解更多信息,参见模板语法中的NgSwitch指令部分。

Back to top

回到顶部

Filters/pipes

过滤器/管道

Angular pipes provide formatting and transformation for data in the template, similar to AngularJS filters. Many of the built-in filters in AngularJS have corresponding pipes in Angular. For more information on pipes, see Pipes.

Angular中的管道为模板提供了格式化和数据转换功能,类似于AngularJS中的过滤器。 AngularJS中的很多内置过滤器在Angular中都有对应的管道。 要了解管道的更多信息,参见Pipes

AngularJSAngular

currency

<td>{{movie.price | currency}}</td>

Formats a number as currency.

把一个数字格式化成货币。

currency

<td>{{movie.price | currency:'USD':true}}</td>

The Angular currency pipe is similar although some of the parameters have changed.

Angular的currency管道和1中很相似,只是有些参数变化了。

date

<td>{{movie.releaseDate | date}}</td>

Formats a date to a string based on the requested format.

基于要求的格式把日期格式化成字符串。

date

<td>{{movie.releaseDate | date}}</td>

The Angular date pipe is similar.

Angular的date管道和·中很相似。参见备注来了解字符串日期值。

filter

<tr ng-repeat="movie in movieList | filter: {title:listFilter}">

Selects a subset of items from the defined collection, based on the filter criteria.

基于过滤条件从指定的集合中选取出一个子集。

none

没了

For performance reasons, no comparable pipe exists in Angular. Do all your filtering in the component. If you need the same filtering code in several templates, consider building a custom pipe.

在Angular中,出于性能的考虑,并没有一个类似的管道。 过滤逻辑应该在组件中用代码实现。 如果它将被复用在几个模板中,可以考虑构建一个自定义管道。

json

<pre>{{movie | json}}</pre>

Converts a JavaScript object into a JSON string. This is useful for debugging.

把一个JavaScript对象转换成一个JSON字符串。这对调试很有用。

json

<pre>{{movie | json}}</pre>

The Angular json pipe does the same thing.

Angular的json管道做完全相同的事。

limitTo

<tr ng-repeat="movie in movieList | limitTo:2:0">

Selects up to the first parameter (2) number of items from the collection starting (optionally) at the beginning index (0).

从集合中选择从(第二参数指定的)起始索引号(0)开始的最多(第一参数指定的)条目数(2)个条目。

slice

<tr *ngFor="let movie of movies | slice:0:2">

The SlicePipe does the same thing but the order of the parameters is reversed, in keeping with the JavaScript Slice method. The first parameter is the starting index; the second is the limit. As in AngularJS, coding this operation within the component instead could improve performance.

SlicePipe做同样的事,但是两个参数的顺序是相反的,以便于JavaScript中的slice方法保持一致。 第一个参数是起始索引号,第二个参数是限制的数量。 和AngularJS中一样,如果们改用组件中的代码实现此操作,性能将会提升。

lowercase

<div>{{movie.title | lowercase}}</div>

Converts the string to lowercase.

把该字符串转成小写形式。

lowercase

<td>{{movie.title | lowercase}}</td>

The Angular lowercase pipe does the same thing.

Angular的lowercase管道和1中的功能完全相同。

number

<td>{{movie.starRating | number}}</td>

Formats a number as text.

把数字格式化为文本。

number

<td>{{movie.starRating | number}}</td> <td>{{movie.starRating | number:'1.1-2'}}</td> <td>{{movie.approvalRating | percent: '1.0-2'}}</td>

The Angular number pipe is similar. It provides more functionality when defining the decimal places, as shown in the second example above.

Angular的number管道很相似。 但在指定小数点位置时,它提供了更多的功能,如第二个范例所示。

Angular also has a percent pipe, which formats a number as a local percentage as shown in the third example.

Angular还有一个percent管道,它把一个数组格式化为本地化的(local)百分比格式,如第三个范例所示。

orderBy

<tr ng-repeat="movie in movieList | orderBy : 'title'">

Displays the collection in the order specified by the expression. In this example, the movie title orders the movieList.

使用表达式中所指定的方式对集合进行排序。 在这个例子中,movieList被根据movie的title排序了。

none

没了

For performance reasons, no comparable pipe exists in Angular. Instead, use component code to order or sort results. If you need the same ordering or sorting code in several templates, consider building a custom pipe.

在Angular中,出于性能的考虑,并没有一个类似的管道。 排序逻辑应该在组件中用代码实现。 如果它将被复用在几个模板中,可以考虑构建一个自定义管道。

Back to top

回到顶部

Modules/controllers/components

模块/控制器/组件

In both AngularJS and Angular, Angular modules help you organize your application into cohesive blocks of functionality.

无论在AngularJS还是Angular中,我们都要借助“模块”来把应用拆分成一些紧密相关的功能块。

In AngularJS, you write the code that provides the model and the methods for the view in a controller. In Angular, you build a component.

在AngularJS中,我们在控制器中写代码,来为视图提供模型和方法。 在Angular中,我们创建组件

Because much AngularJS code is in JavaScript, JavaScript code is shown in the AngularJS column. The Angular code is shown using TypeScript.

因为很多AngularJS的代码是用JavaScript写的,所以在AngularJS列显示的是JavaScript代码,而Angular列显示的是TypeScript代码。

AngularJSAngular

IIFE

(function () { ... }());

In AngularJS, an immediately invoked function expression (or IIFE) around controller code keeps it out of the global namespace.

在AngularJS中,用立即调用的函数表达式(IIFE)来包裹控制器代码可以让控制器代码不会污染全局命名空间。

none

没了

This is a nonissue in Angular because ES 2015 modules handle the namespacing for you.

在Angular中我们不用担心这个问题,因为使用ES 2015的模块,模块会替我们处理命名空间问题。

For more information on modules, see the Modules section of the Architecture Overview.

要了解关于模块的更多信息,参见架构概览中的模块部分。

Angular modules

Angular模块

angular.module("movieHunter", ["ngRoute"]);

In AngularJS, an Angular module keeps track of controllers, services, and other code. The second argument defines the list of other modules that this module depends upon.

在AngularJS中,Angular模块用来对控制器、服务和其它代码进行跟踪。第二个参数定义该模块依赖的其它模块列表。

Angular modules

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }

Angular modules, defined with the NgModule decorator, serve the same purpose:

Angular的模块用NgModule装饰器进行定义,有如下用途:

  • imports: specifies the list of other modules that this module depends upon

    imports: 指定当前模块依赖的其它模块列表

  • declaration: keeps track of your components, pipes, and directives.

    declaration: 用于记录组件、管道和指令。

For more information on modules, see Angular Modules.

要了解关于模块的更多知识,参见Angular Modules

Controller registration

控制器注册

angular .module("movieHunter") .controller("MovieListCtrl", ["movieService", MovieListCtrl]);

AngularJS has code in each controller that looks up an appropriate Angular module and registers the controller with that module.

在AngularJS中,在每个控制器中都有一些代码,用于找到合适的Angular模块并把该控制器注册进去。

The first argument is the controller name. The second argument defines the string names of all dependencies injected into this controller, and a reference to the controller function.

第一个参数是控制器的名称,第二个参数定义了所有将注入到该控制器的依赖的字符串名称,以及一个到控制器函数的引用。

Component decorator

组件装饰器

@Component({ selector: 'movie-list', templateUrl: './movie-list.component.html', styleUrls: [ './movie-list.component.css' ], })

Angular adds a decorator to the component class to provide any required metadata. The @Component decorator declares that the class is a component and provides metadata about that component such as its selector (or tag) and its template.

在Angular中,我们往组件类上添加了一个装饰器,以提供任何需要的元数据。 @Component装饰器把该类声明为组件,并提供了关于该组件的元数据,比如它的选择器(或标签)和模板。

This is how you associate a template with logic, which is defined in the component class.

这就是把模板关联到代码的方式,它定义在组件类中。

For more information, see the Components section of the Architecture Overview page.

要了解关于模板的更多信息,参见架构概览中的组件部分。

Controller function

控制器函数

function MovieListCtrl(movieService) { }

In AngularJS, you write the code for the model and methods in a controller function.

在Angular1中,我们在控制器函数中写模型和方法的代码。

Component class

组件类

export class MovieListComponent { }

In Angular, you create a component class.

在Angular中,我们写组件类。

NOTE: If you are using TypeScript with AngularJS, you must use the export keyword to export the component class.

注意:如果你正在用TypeScript写AngularJS,那么必须用export关键字来导出组件类。

For more information, see the Components section of the Architecture Overview page.

要了解关于组件的更多信息,参见架构概览中的组件部分。

Dependency injection

依赖注入

MovieListCtrl.$inject = ['MovieService']; function MovieListCtrl(movieService) { }

In AngularJS, you pass in any dependencies as controller function arguments. This example injects a MovieService.

在AngularJS中,我们把所有依赖都作为控制器函数的参数。 在这个例子中,我们注入了一个MovieService

To guard against minification problems, tell Angular explicitly that it should inject an instance of the MovieService in the first parameter.

我们还通过在第一个参数明确告诉Angular它应该注入一个MovieService的实例,以防止在最小化时出现问题。

Dependency injection

依赖注入

constructor(movieService: MovieService) { }

In Angular, you pass in dependencies as arguments to the component class constructor. This example injects a MovieService. The first parameter's TypeScript type tells Angular what to inject, even after minification.

在Angular中,我们把依赖作为组件构造函数的参数传入。 在这个例子中,我们注入了一个MovieService。 即使在最小化之后,第一个参数的TypeScript类型也会告诉Angular它该注入什么。

For more information, see the Dependency injection section of the Architecture Overview.

要了解关于依赖注入的更多信息,参见架构概览中的依赖注入部分。

Back to top

回到顶部

Style sheets

样式表

Style sheets give your application a nice look. In AngularJS, you specify the style sheets for your entire application. As the application grows over time, the styles for the many parts of the application merge, which can cause unexpected results. In Angular, you can still define style sheets for your entire application. But now you can also encapsulate a style sheet within a specific component.

样式表美化我们的应用程序。 在AngularJS中,我们为整个应用程序指定样式表。 当应用程序成长一段时间之后,应用程序中很多部分的样式会被合并,导致无法预计的后果。 在Angular中,我们仍然会为整个应用程序定义样式,不过现在也可以把样式表封装在特定的组件中。

AngularJSAngular
<link href="styles.css" rel="stylesheet" />

AngularJS, uses a link tag in the head section of the index.html file to define the styles for the application.

在AngularJS中,我们在index.htmlhead区使用link标签来为应用程序定义样式。

<link rel="stylesheet" href="styles.css">

In Angular, you can continue to use the link tag to define the styles for your application in the index.html file. But now you can also encapsulate styles for your components.

在Angular2中,我们可以继续在index.html中使用link标签来为应用程序定义样式。 但是也能在组件中封装样式。

StyleUrls

In Angular, you can use the styles or styleUrls property of the @Component metadata to define a style sheet for a particular component.

在Angular中,我们可以在@Component的元数据中使用stylesstyleUrls属性来为一个特定的组件定义样式表。

styleUrls: [ './movie-list.component.css' ],

This allows you to set appropriate styles for individual components that won’t leak into other parts of the application.

这让我们可以为各个组件设置合适的样式,而不用担心它被泄漏到程序中的其它部分。

Back to top

回到顶部