关于AngularJS 与 Angular 概念的快速参考link

AngularJS to Angular Concepts: Quick Referencelink关于AngularJS 与 Angular 概念的快速参考linkAngular is the name for the Angular of today and tomorrow. AngularJS is the name for all v1.x versions of Angular.

Angular 这个名字专指现在和未来的 Angular 版本,而 AngularJS 专指 Angular 的所有 v1.x 版本。

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

本章提供了一个快速的参考指南,指出一些常用的 AngularJS 语法及其在 Angular 中的等价物。

See the Angular syntax in this.

参见以学习 Angular 语法

Template basicslink模板基础linkTemplates 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 中的等价语法。

AngularJS

Angular

Bindings/interpolationlink绑定/插值表达式linkYour 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/interpolationlink绑定/插值表达式linkYour 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.

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

Filterslink过滤器link{{movie.title | uppercase}}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 属性过滤成了大写形式。

Pipeslink管道link{{movie.title | uppercase}}

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 variableslink局部变量link {{movie.title}} Here, movie is a user-defined local variable.

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

Input variableslink输入变量link {{movie.title}}

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 微语法部分。

Template directiveslink模板指令linkAngularJS 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 中的等价物。

AngularJS

Angular

ng-applinkThe 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)就可以了。

Bootstrappinglink引导linkimport { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } platformBrowserDynamic().bootstrapModule(AppModule);

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)来启动应用。

ng-classlink

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.

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

ngClasslink

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-clicklink