# Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/change-log.md # aio/content/guide/component-styles.md # aio/content/guide/deployment.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/pipes.md # aio/content/guide/reactive-forms.md # aio/content/guide/router.md # aio/content/guide/security.md # aio/content/guide/set-document-title.md # aio/content/guide/setup-systemjs-anatomy.md # aio/content/guide/setup.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/upgrade.md # aio/content/marketing/docs.md # aio/content/marketing/features.html # aio/content/marketing/resources.json # aio/content/navigation.json # aio/src/app/layout/doc-viewer/doc-viewer.component.ts
		
			
				
	
	
	
		
			28 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	Cheat Sheet
速查表
| Bootstrapping 启动 | 
 | 
|---|---|
| platformBrowserDynamic().bootstrapModule(AppModule); | Bootstraps the app, using the root component from the specified  用  | 
| NgModules Angular 模块 | 
 | 
|---|---|
| @NgModule({ declarations: ..., imports: ..., | Defines a module that contains components, directives, pipes, and providers. 定义一个模块,其中可以包含组件、指令、管道和服务提供商。 | 
| declarations: [MyRedComponent, MyBlueComponent, MyDatePipe] | List of components, directives, and pipes that belong to this module. 属于当前模块的组件、指令和管道的列表。 | 
| imports: [BrowserModule, SomeOtherModule] | List of modules to import into this module. Everything from the imported modules
is available to  本模块所导入的模块列表 | 
| exports: [MyRedComponent, MyDatePipe] | List of components, directives, and pipes visible to modules that import this module. 那些导入了本模块的模块所能看到的组件、指令和管道的列表 | 
| providers: [MyService, { provide: ... }] | List of dependency injection providers visible both to the contents of this module and to importers of this module. 依赖注入提供商的列表,本模块以及本模块导入的所有模块中的内容都可以看见它们。 | 
| bootstrap: [MyAppComponent] | List of components to bootstrap when this module is bootstrapped. 当本模块启动时,随之启动的组件列表。 | 
| Template syntax 模板语法 | |
|---|---|
| <input [value]="firstName"> | Binds property  把 | 
| <div [attr.role]="myAriaRole"> | Binds attribute  把属性(Attribute) | 
| <div [class.extra-sparkle]="isDelightful"> | Binds the presence of the CSS class  根据 | 
| <div [style.width.px]="mySize"> | Binds style property  把CSS样式属性 | 
| <button (click)="readRainbow($event)"> | Calls method  当这个按钮元素(及其子元素)上的click事件触发时,调用方法 | 
| <div title="Hello {{ponyName}}"> | Binds a property to an interpolated string, for example, "Hello Seabiscuit". Equivalent to:
 把一个属性绑定到插值字符串(如"Hello Seabiscuit")。这种写法等价于 | 
| <p>Hello {{ponyName}}</p> | Binds text content to an interpolated string, for example, "Hello Seabiscuit". 把文本内容绑定到插值字符串(如"Hello Seabiscuit") | 
| <my-cmp [(title)]="name"> | Sets up two-way data binding. Equivalent to:  设置双向绑定。等价于 | 
| <video #movieplayer ...> | Creates a local variable  创建一个局部变量 | 
| <p *myUnless="myExpression">...</p> | The  星号 | 
| <p>Card No.: {{cardNumber | myCardNumberFormatter}}</p> | Transforms the current value of expression  使用名叫 | 
| <p>Employer: {{employer?.companyName}}</p> | The safe navigation operator ( 安全导航操作符( | 
| <svg:rect x="0" y="0" width="100" height="100"/> | An SVG snippet template needs an  模板中的 SVG 片段需要给它的根元素加上 | 
| <svg> | An  以 | 
| Built-in directives 内置指令 | 
 | 
|---|---|
| <section *ngIf="showSection"> | Removes or recreates a portion of the DOM tree based on the  根据 | 
| <li *ngFor="let item of list"> | Turns the li element and its contents into a template, and uses that to instantiate a view for each item in list. 把li元素及其内容变成一个模板,并使用这个模板为列表中的每一个条目实例化一个视图。 | 
| <div [ngSwitch]="conditionExpression"> | Conditionally swaps the contents of the div by selecting one of the embedded templates based on the current value of  根据 | 
| <div [ngClass]="{'active': isActive, 'disabled': isDisabled}"> | Binds the presence of CSS classes on the element to the truthiness of the associated map values. The right-hand expression should return {class-name: true/false} map. 根据 map 中的 value 是否为真,来决定该元素上是否出现与 name 对应的 CSS 类。右侧的表达式应该返回一个形如  | 
| <div [ngStyle]="{'property': 'value'}"><div [ngStyle]="dynamicStyles()"> | Allows you to assign styles to an HTML element using CSS. You can use CSS directly, as in the first example, or you can call a method from the component. | 
| Forms 表单 | 
 | 
|---|---|
| <input [(ngModel)]="userName"> | Provides two-way data-binding, parsing, and validation for form controls. 为表单控件提供双向数据绑定、解析和验证功能。 | 
| Class decorators 类装饰器(decorator) | 
 | 
|---|---|
| @Component({...}) | Declares that a class is a component and provides metadata about the component. 声明一个类是组件,并提供该组件的元数据。 | 
| @Directive({...}) | Declares that a class is a directive and provides metadata about the directive. 声明一个类是指令,并提供该指令的元数据。 | 
| @Pipe({...}) | Declares that a class is a pipe and provides metadata about the pipe. 声明一个类是管道,并提供该管道的元数据。 | 
| @Injectable() | Declares that a class has dependencies that should be injected into the constructor when the dependency injector is creating an instance of this class. 声明一个类具有一些依赖,当依赖注入器试图创建该类的实例时,应该把这些依赖注入到该类的构造函数中。 | 
| Directive configuration 指令配置 | 
 | 
|---|---|
| selector: '.cool-button:not(a)' | Specifies a CSS selector that identifies this directive within a template. Supported selectors include  指定一个 CSS 选择器,用于在模板中标记出该指令。支持的选择器类型包括: Does not support parent-child relationship selectors. 但不支持指定父子关系的选择器。 | 
| providers: [MyService, { provide: ... }] | List of dependency injection providers for this directive and its children. 该指令及其子指令的依赖注入提供商列表。 | 
| Component configuration 组件配置 | 
 
 | 
|---|---|
| moduleId: module.id | If set, the  如果设置了,那么  | 
| viewProviders: [MyService, { provide: ... }] | List of dependency injection providers scoped to this component's view. 依赖注入提供商列表,但它们的范围被限定为当前组件的视图。 | 
| template: 'Hello {{name}}' | Inline template or external template URL of the component's view. 当前组件视图的内联模板或外部模板的 URL 。 | 
| styles: ['.primary {color: red}'] | List of inline CSS styles or external stylesheet URLs for styling the component’s view. 用于为当前组件的视图提供样式的内联 CSS 或外部样式表 URL 的列表。 | 
| Class field decorators for directives and components 给指令或组件类用的属性装饰器 | 
 | 
|---|---|
| @Input() myProperty; | Declares an input property that you can update via property binding (example:
 声明一个输入属性,你可以通过属性绑定来更新它,如  | 
| @Output() myEvent = new EventEmitter(); | Declares an output property that fires events that you can subscribe to with an event binding (example:  声明一个输出属性,它发出事件,你可以用事件绑定来订阅它们(如: | 
| @HostBinding('class.valid') isValid; | Binds a host element property (here, the CSS class  把宿主元素的一个属性(这里是 CSS 类  | 
| @HostListener('click', ['$event']) onClick(e) {...} | Subscribes to a host element event ( 用指令或组件上的 | 
| @ContentChild(myPredicate) myChildComponent; | Binds the first result of the component content query ( 把组件内容查询( | 
| @ContentChildren(myPredicate) myChildComponents; | Binds the results of the component content query ( 把组件内容查询( | 
| @ViewChild(myPredicate) myChildComponent; | Binds the first result of the component view query ( 把组件视图查询( | 
| @ViewChildren(myPredicate) myChildComponents; | Binds the results of the component view query ( 把组件视图查询( | 
| Directive and component change detection and lifecycle hooks 指令和组件的变更检测与生命周期钩子 | (implemented as class methods) 由类的方法实现。 | 
|---|---|
| constructor(myService: MyService, ...) { ... } | Called before any other lifecycle hook. Use it to inject dependencies, but avoid any serious work here. 在任何其它生命周期钩子之前调用。可以用它来注入依赖项,但不要在这里做正事。 | 
| ngOnChanges(changeRecord) { ... } | Called after every change to input properties and before processing content or child views. 每当输入属性发生变化时就会调用,但位于处理内容( | 
| ngOnInit() { ... } | Called after the constructor, initializing input properties, and the first call to  在调用完构造函数、初始化完所有输入属性并首次调用过 | 
| ngDoCheck() { ... } | Called every time that the input properties of a component or a directive are checked. Use it to extend change detection by performing a custom check. 每当对组件或指令的输入属性进行变更检测时就会调用。可以用它来扩展变更检测逻辑,执行自定义的检测逻辑。 | 
| ngAfterContentInit() { ... } | Called after  
 | 
| ngAfterContentChecked() { ... } | Called after every check of the component's or directive's content. 每当组件或指令的内容( | 
| ngAfterViewInit() { ... } | Called after  当 | 
| ngAfterViewChecked() { ... } | Called after every check of the component's view. Applies to components only. 当组件视图每次执行变更检测时调用。只适用于组件。 | 
| ngOnDestroy() { ... } | Called once, before the instance is destroyed. 只在实例被销毁前调用一次。 | 
| Dependency injection configuration 依赖注入的配置 | |
|---|---|
| { provide: MyService, useClass: MyMockService } | Sets or overrides the provider for  把  | 
| { provide: MyService, useFactory: myFactory } | Sets or overrides the provider for  把  | 
| { provide: MyValue, useValue: 41 } | Sets or overrides the provider for  把  | 
| Routing and navigation 路由与导航 | 
 | 
|---|---|
| const routes: Routes = [ | Configures routes for the application. Supports static, parameterized, redirect, and wildcard routes. Also supports custom route data and resolve. 为该应用配置路由。支持静态、参数化、重定向和通配符路由。也支持自定义路由数据和解析(resolve)函数。 | 
|  | Marks the location to load the component of the active route. 标记出一个位置,用来加载活动路由的组件。 | 
|  | Creates a link to a different view based on a route instruction consisting of a route path, required and optional parameters, query parameters, and a fragment. To navigate to a root route, use the  使用路由体系创建一个到其它视图的链接。路由体系由路由路径、必要参数、可选参数、查询参数和文档片段组成。要导航到根路由,请使用 | 
| <a [routerLink]="[ '/path' ]" routerLinkActive="active"> | The provided classes are added to the element when the  当  | 
| class CanActivateGuard implements CanActivate { | An interface for defining a class that the router should call first to determine if it should activate this component. Should return a boolean or an Observable/Promise that resolves to a boolean. 用来定义类的接口。路由器会首先调用本接口来决定是否激活该路由。应该返回一个  | 
| class CanDeactivateGuard implements CanDeactivate<T> { | An interface for defining a class that the router should call first to determine if it should deactivate this component after a navigation. Should return a boolean or an Observable/Promise that resolves to a boolean. 用来定义类的接口。路由器会在导航离开前首先调用本接口以决定是否取消激活本路由。应该返回一个  | 
| class CanActivateChildGuard implements CanActivateChild { | An interface for defining a class that the router should call first to determine if it should activate the child route. Should return a boolean or an Observable/Promise that resolves to a boolean. 用来定义类的接口。路由器会首先调用本接口来决定是否激活一个子路由。应该返回一个  | 
| class ResolveGuard implements Resolve<T> { | An interface for defining a class that the router should call first to resolve route data before rendering the route. Should return a value or an Observable/Promise that resolves to a value. 用来定义类的接口。路由器会在渲染该路由之前,首先调用它来解析路由数据。应该返回一个值或能解析成值的  | 
| class CanLoadGuard implements CanLoad { | An interface for defining a class that the router should call first to check if the lazy loaded module should be loaded. Should return a boolean or an Observable/Promise that resolves to a boolean. 用来定义类的接口。路由器会首先调用它来决定是否应该加载一个惰性加载模块。应该返回一个  |