关于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
要在 AngularJS 中过滤输出,使用管道字符(|)以及一个或多个过滤器。
This example filters the title property to uppercase.
这个例子中把 title 属性过滤成了大写形式。
Pipeslink管道link
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 是一个用户定义的局部变量
Input variableslink输入变量link
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-applink
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)就可以了。
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
在 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
在 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 eventlink绑定到 click 事件link
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-controllerlink
在 AngularJS 中,ng-controller 指令把控制器附加到视图上。 使用 ng-controller(或把控制器定义为路由的一部分)把视图及其控制器的代码联系在一起。
Component decoratorlink组件装饰器link@Component({ selector: 'app-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-hidelinkIn 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 propertylink绑定到 hidden 属性linkIn Angular, you use property binding; there is no built-in hide directive. For more information, see ng-show.
在 Angular 中,并没有一个内置的 hide 指令,可以改用属性绑定。 参见ng-show了解更多。
ng-hreflinkAngular DocsThe 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 通常用来作为导航的一部分,激活一个路由。
MoviesRouting is handled differently in Angular.
路由在 Angular 中的处理方式不同。
Bind to the href propertylink绑定到 href 属性linkAngular Docs
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 指令。
For more information on routing, see the RouterLink binding section of the Routing & Navigation page.
要了解关于路由的更多信息,请参见路由与导航的RouterLink 绑定部分。
ng-iflink
{{movie.price | currency}} | Formats a number as currency.{{movie.price | currency:'USD':true}} | {{movie.releaseDate | date}} | Formats a date to a string based on the requested format.{{movie.releaseDate | date}} | ||||||
{{movie.title | lowercase}} | Converts the string to lowercase.{{movie.title | lowercase}} | {{movie.starRating | number}} | Formats a number as text.{{movie.starRating | number}} | {{movie.starRating | number:'1.1-2'}} | {{movie.approvalRating | percent: '1.0-2'}} | ||||