include ../_util-fns
a(id="top")
:marked
_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中的等价物。
:marked
**See the Angular syntax in this
+makeExample('cb-ajs-quick-reference/ts/src/app/app.module.1.ts','','app.module.ts')(format="." )
:marked
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](../guide/setup.html) page.
要了解更多,参见[搭建本地开发环境](../guide/setup.html)。
tr(style=top)
td
:marked
### ng-class
code-example(format="").
<div ng-class="{active: isActive}">
<div ng-class="{active: isActive,
shazam: isImportant}">
:marked
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类。
td
:marked
### ngClass
+makeExample('cb-ajs-quick-reference/ts/src/app/app.component.html', 'ngClass')(format="." )
:marked
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](../guide/template-syntax.html#other-bindings)
section of the [Template Syntax](../guide/template-syntax.html) page.
要了解更多信息,参见[模板语法](../guide/template-syntax.html)中的[属性、CSS类和样式绑定](../guide/template-syntax.html#other-bindings)部分。
tr(style=top)
td
:marked
### ng-click
code-example(format="").
<button ng-click="vm.toggleImage()">
<button ng-click="vm.toggleImage($event)">
:marked
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`对象,它提供了事件的详情,并被传到控制器。
td
:marked
### Bind to the `click` event
### 绑定到`click`事件
+makeExample('cb-ajs-quick-reference/ts/src/app/app.component.html', 'event-binding')(format="." )
:marked
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事件的列表,请参见[网络事件](https://developer.mozilla.org/en-US/docs/Web/Events)。
For more information, see the [Event binding](../guide/template-syntax.html#event-binding)
section of the [Template Syntax](../guide/template-syntax.html) page.
要了解更多,请参见[模板语法](../guide/template-syntax.html)中的[事件绑定](../guide/template-syntax.html#event-binding)部分。
tr(style=top)
td
:marked
### ng-controller
code-example(format="").
<div ng-controller="MovieListCtrl as vm">
:marked
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`(或把控制器定义为路由的一部分)把视图及其控制器的代码联系在一起。
td
:marked
### Component decorator
### Component装饰器
+makeExample('cb-ajs-quick-reference/ts/src/app/movie-list.component.ts', 'component')(format="." )
:marked
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](../guide/architecture.html#component).
要了解更多,请参见[架构概览](../guide/architecture.html#component)。
tr(style=top)
td
:marked
### 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](#ng-show).
在AngularJS中,`ng-hide`指令会基于一个表达式显示或隐藏相关的HTML元素。
参见[ng-show](#ng-show)了解更多。
td
:marked
### 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](#ng-show).
在Angular中,并没有一个内置的*hide*指令,可以改用属性绑定。
参见[ng-show](#ng-show)了解更多。
tr(style=top)
td
:marked
### ng-href
code-example(format="").
<a ng-href="angularDocsUrl">Angular Docs</a>
:marked
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`通常用来作为导航的一部分,激活一个路由。
code-example(format="").
<a ng-href="#movies">Movies</a>
:marked
Routing is handled differently in Angular.
路由在Angular中的处理方式不同。
td
:marked
### Bind to the `href` property
### 绑定到`href`属性
+makeExample('cb-ajs-quick-reference/ts/src/app/app.component.html', 'href')(format="." )
:marked
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](../guide/template-syntax.html#property-binding)
section of the [Template Syntax](../guide/template-syntax.html) page.
要了解[属性绑定](../guide/template-syntax.html#property-binding)的更多知识,参见[模板语法](../guide/template-syntax.html)。
In Angular, `href` is no longer used for routing. Routing uses `routerLink`, as shown in the following example.
在Angular中,`href`不再用作路由,而是改用第三个例子中所展示的`routerLink`指令。
+makeExample('cb-ajs-quick-reference/ts/src/app/app.component.html', 'router-link')(format="." )
:marked
For more information on routing, see the [RouterLink binding](../guide/router.html#router-link)
section of the [Routing & Navigation](../guide/router.html) page.
要了解关于路由的更多信息,请参见[路由与导航](../guide/router.html)的[RouterLink绑定](../guide/router.html#router-link)部分。
tr(style=top)
td
:marked
### ng-if
code-example(format="").
<table ng-if="movies.length">
:marked
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 `