速查表
Cheat Sheet
引导/启动 Bootstrapping |
|
---|---|
platformBrowserDynamic().bootstrapModule(AppModule); | 用 Bootstraps the app, using the root component from the specified |
NgModules |
|
---|---|
| 定义一个模块,其中可以包含组件、指令、管道和服务提供者。 Defines a module that contains components, directives, pipes, and providers. |
| 属于当前模块的组件、指令和管道的列表。 List of components, directives, and pipes that belong to this module. |
| 本模块所导入的模块列表 List of modules to import into this module. Everything from the imported modules is available to |
| 那些导入了本模块的模块所能看到的组件、指令和管道的列表 List of components, directives, and pipes visible to modules that import this module. |
| 依赖注入提供者的列表,本模块以及本模块导入的所有模块中的内容都可以看见它们。 List of dependency injection providers visible both to the contents of this module and to importers of this module. |
entryComponents: [SomeComponent, OtherComponent] | 任何未在可达模板中引用过的组件列表,比如从代码中动态创建的。 List of components not referenced in any reachable template, for example dynamically created from code. |
| 当本模块启动时,随之启动的组件列表。 List of components to bootstrap when this module is bootstrapped. |
模板语法 Template syntax | |
---|---|
| 把 Binds property |
| 把属性(Attribute) Binds attribute |
| 根据 Binds the presence of the CSS class |
| 把 CSS 样式属性 Binds style property |
| 当这个按钮元素(及其子元素)上的 click 事件触发时,调用方法 Calls method |
| 把一个属性绑定到插值字符串(如"Hello Seabiscuit")。这种写法等价于 Binds a property to an interpolated string, for example, "Hello Seabiscuit". Equivalent to: |
| 把文本内容绑定到插值字符串(如"Hello Seabiscuit") Binds text content to an interpolated string, for example, "Hello Seabiscuit". |
| 设置双向绑定。等价于 Sets up two-way data binding. Equivalent to: |
| 创建一个局部变量 Creates a local variable |
| 这个 The |
| 使用名叫 Transforms the current value of expression |
| 安全导航操作符( The safe navigation operator ( |
| 模板中的 SVG 片段需要给它的根元素加上 An SVG snippet template needs an |
| 以 An |
内置指令 Built-in directives |
|
---|---|
| 根据 Removes or recreates a portion of the DOM tree based on the |
| 把 li 元素及其内容变成一个模板,并使用这个模板为列表中的每一个条目实例化一个视图。 Turns the li element and its contents into a template, and uses that to instantiate a view for each item in list. |
| 根据 Conditionally swaps the contents of the div by selecting one of the embedded templates based on the current value of |
| 根据 map 中的 value 是否为真,来决定该元素上是否出现与 name 对应的 CSS 类。右侧的表达式应该返回一个形如 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. |
| 允许你使用 CSS 为 HTML 元素指定样式。你可以像第一个例子那样直接使用 CSS,也可以调用组件中的方法。 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 |
|
---|---|
| 为表单控件提供双向数据绑定、解析和验证功能。 Provides two-way data-binding, parsing, and validation for form controls. |
类装饰器 Class decorators |
|
---|---|
| 声明一个类是组件,并提供该组件的元数据。 Declares that a class is a component and provides metadata about the component. |
| 声明一个类是指令,并提供该指令的元数据。 Declares that a class is a directive and provides metadata about the directive. |
| 声明一个类是管道,并提供该管道的元数据。 Declares that a class is a pipe and provides metadata about the pipe. |
| 声明某个类可以注册为提供者,并能被另一个类注入。如果没有该装饰器,编译器就不会生成足够的元数据,当它被注入到别处时,就无法正常创建该类。 Declares that a class can be provided and injected by other classes. Without this decorator, the compiler won't generate enough metadata to allow the class to be created properly when it's injected somewhere. |
指令配置项 Directive configuration |
|
---|---|
| 指定一个 CSS 选择器,用于在模板中标记出该指令。支持的选择器类型包括: Specifies a CSS selector that identifies this directive within a template. Supported selectors include 但不支持指定父子关系的选择器。 Does not support parent-child relationship selectors. |
| 该指令及其子指令的依赖注入提供者列表。 List of dependency injection providers for this directive and its children. |
组件配置项 Component configuration |
|
---|---|
| 如果设置了,那么 If set, the |
| 依赖注入提供者列表,但它们的范围被限定为当前组件的视图。 List of dependency injection providers scoped to this component's view. |
| 当前组件视图的内联模板或外部模板的 URL 。 Inline template or external template URL of the component's view. |
| 用于为当前组件的视图提供样式的内联 CSS 或外部样式表 URL 的列表。 List of inline CSS styles or external stylesheet URLs for styling the component’s view. |
给指令和组件使用的类属性配置项 Class field decorators for directives and components |
|
---|---|
| 声明一个输入属性,你可以通过属性绑定来更新它,如 Declares an input property that you can update via property binding (example: |
| 声明一个输出属性,它发出事件,你可以用事件绑定来订阅它们(如: Declares an output property that fires events that you can subscribe to with an event binding (example: |
| 把宿主元素的一个属性(这里是 CSS 类 Binds a host element property (here, the CSS class |
| 用指令或组件上的 Subscribes to a host element event ( |
| 把组件内容查询( Binds the first result of the component content query ( |
| 把组件内容查询( Binds the results of the component content query ( |
| 把组件视图查询( Binds the first result of the component view query ( |
| 把组件视图查询( Binds the results of the component view query ( |
指令与组件的变更检测与生命周期钩子 Directive and component change detection and lifecycle hooks | (作为类的方法实现。) (implemented as class methods) |
---|---|
| 在任何其它生命周期钩子之前调用。可以用它来注入依赖项,但不要在这里做正事。 Called before any other lifecycle hook. Use it to inject dependencies, but avoid any serious work here. |
| 每当输入属性发生变化时就会调用,但位于处理内容( Called after every change to input properties and before processing content or child views. |
| 在调用完构造函数、初始化完所有输入属性并首次调用过 Called after the constructor, initializing input properties, and the first call to |
| 每当对组件或指令的输入属性进行变更检测时就会调用。可以用它来扩展变更检测逻辑,执行自定义的检测逻辑。 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. |
|
Called after |
| 每当组件或指令的内容( Called after every check of the component's or directive's content. |
| 当 Called after |
| 当组件的视图及其子视图或指令所属的视图每次执行变更检测时调用。 Called after every check of the component's views and child views / the view that a directive is in. |
| 只在实例被销毁前调用一次。 Called once, before the instance is destroyed. |
依赖注入配置项 Dependency injection configuration | |
---|---|
| 把 Sets or overrides the provider for |
| 把 Sets or overrides the provider for |
| 把 Sets or overrides the provider for |
路由与导航 Routing and navigation |
|
---|---|
| 为该应用配置路由。支持静态、参数化、重定向和通配符路由。也支持自定义路由数据和解析(resolve)函数。 Configures routes for the application. Supports static, parameterized, redirect, and wildcard routes. Also supports custom route data and 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 |
| 当 The provided classes are added to the element when the |
| 用来定义类的接口。路由器会首先调用本接口来决定是否激活该路由。应该返回一个 An interface for defining a class that the router should call first to determine if it should activate this component. Should return a boolean|UrlTree or an Observable/Promise that resolves to a boolean|UrlTree. |
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|UrlTree or an Observable/Promise that resolves to a boolean|UrlTree. |
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|UrlTree or an Observable/Promise that resolves to a boolean|UrlTree. |
| 用来定义类的接口。路由器会在渲染该路由之前,首先调用它来解析路由数据。应该返回一个值或能解析成值的 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. |
| 用来定义类的接口。路由器会首先调用它来决定是否应该加载一个惰性加载模块。应该返回一个 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|UrlTree or an Observable/Promise that resolves to a boolean|UrlTree. |