填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

ApplicationRef

对页面上运行的 Angular 应用程序的引用。

A reference to an Angular application running on a page.

      
      class ApplicationRef {
  componentTypes: Type<any>[]
  components: ComponentRef<any>[]
  isStable: Observable<boolean>
  viewCount
  bootstrap<C>(componentOrFactory: ComponentFactory<C> | Type<C>, rootSelectorOrNode?: any): ComponentRef<C>
  tick(): void
  attachView(viewRef: ViewRef): void
  detachView(viewRef: ViewRef): void
}
    

属性

属性说明
componentTypes: Type<any>[]只读

获取注册到该应用程序的组件类型的列表。在创建组件之前,会填充此列表。

Get a list of component types registered to this application. This list is populated even before the component is created.

components: ComponentRef<any>[]只读

获取已注册到该应用中的组件的列表。

Get a list of components registered to this application.

isStable: Observable<boolean>只读

返回一个 Observable,指示应用程序何时变得稳定或不稳定。

Returns an Observable that indicates when the application is stable or unstable.

参见:

  • 用法说明的例子和使用此 API 时的注意事项。

    Usage notes for examples and caveats when using this API.

viewCount只读

返回已附加视图的数量。

Returns the number of attached views.

方法

在应用程序的根级引导新组件。

Bootstrap a new component at the root level of the application.

      
      bootstrap<C>(componentOrFactory: ComponentFactory<C> | Type<C>, rootSelectorOrNode?: any): ComponentRef<C>
    
参数
componentOrFactory ComponentFactory | Type
rootSelectorOrNode any
可选. 默认值是 `undefined`.
返回值

ComponentRef<C>

使用说明

引导过程
Bootstrap process

将新的根组件引导到应用程序时,Angular 将指定的应用程序组件安装到由 componentType 的选择器标识的 DOM 元素上,并启动自动变更检测以完成组件的初始化。

When bootstrapping a new root component into an application, Angular mounts the specified application component onto DOM elements identified by the componentType's selector and kicks off automatic change detection to finish initializing the component.

(可选)可以将组件安装到与 componentType 的选择器不匹配的 DOM 元素上。

Optionally, a component can be mounted onto a DOM element that does not match the componentType's selector.

例子
Example
      
      @Component({selector: 'my-app', template: 'Hello World'})
class MyApp {
}

const myPlatformFactory = createPlatformFactory(platformBrowserDynamic, 'myPlatform');
myPlatformFactory().bootstrapModule(MyApp);
    

调用此方法可以显式处理变更检测及其副作用。

Invoke this method to explicitly process change detection and its side-effects.

      
      tick(): void
    
参数

没有参数。

返回值

void

在开发模式下,tick() 还会执行第二个变更检测周期,以确保没有检测到其他更改。如果在第二个周期内获取了其他更改,则应用程序中的绑定就会产生副作用,这些副作用无法通过一次变更检测过程解决。在这种情况下,Angular 就会引发错误,因为 Angular 应用程序只能进行一次变更检测遍历,在此过程中必须完成所有变更检测。

In development mode, tick() also performs a second change detection cycle to ensure that no further changes are detected. If additional changes are picked up during this second cycle, bindings in the app have side-effects that cannot be resolved in a single change detection pass. In this case, Angular throws an error, since an Angular application can only have one change detection pass during which all change detection must complete.

附加视图,以便对其进行脏检查。视图销毁后将自动分离。如果视图已附加到 ViewContainer,则会抛出此错误。

Attaches a view so that it will be dirty checked. The view will be automatically detached when it is destroyed. This will throw if the view is already attached to a ViewContainer.

      
      attachView(viewRef: ViewRef): void
    
参数
viewRef ViewRef
返回值

void

再次从脏检查中分离视图。

Detaches a view from dirty checking again.

      
      detachView(viewRef: ViewRef): void
    
参数
viewRef ViewRef
返回值

void

使用说明

isStable examples and caveats

Note two important points about isStable, demonstrated in the examples below:

  • the application will never be stable if you start any kind of recurrent asynchronous task when the application starts (for example for a polling process, started with a setInterval, a setTimeout or using RxJS operators like interval);
  • the isStable Observable runs outside of the Angular zone.

Let's imagine that you start a recurrent task (here incrementing a counter, using RxJS interval), and at the same time subscribe to isStable.

      
      constructor(appRef: ApplicationRef) {
  appRef.isStable.pipe(
     filter(stable => stable)
  ).subscribe(() => console.log('App is stable now');
  interval(1000).subscribe(counter => console.log(counter));
}
    

In this example, isStable will never emit true, and the trace "App is stable now" will never get logged.

If you want to execute something when the app is stable, you have to wait for the application to be stable before starting your polling process.

      
      constructor(appRef: ApplicationRef) {
  appRef.isStable.pipe(
    first(stable => stable),
    tap(stable => console.log('App is stable now')),
    switchMap(() => interval(1000))
  ).subscribe(counter => console.log(counter));
}
    

In this example, the trace "App is stable now" will be logged and then the counter starts incrementing every second.

Note also that this Observable runs outside of the Angular zone, which means that the code in the subscription to this Observable will not trigger the change detection.

Let's imagine that instead of logging the counter value, you update a field of your component and display it in its template.

      
      constructor(appRef: ApplicationRef) {
  appRef.isStable.pipe(
    first(stable => stable),
    switchMap(() => interval(1000))
  ).subscribe(counter => this.value = counter);
}
    

As the isStable Observable runs outside the zone, the value field will be updated properly, but the template will not be refreshed!

You'll have to manually trigger the change detection to update the template.

      
      constructor(appRef: ApplicationRef, cd: ChangeDetectorRef) {
  appRef.isStable.pipe(
    first(stable => stable),
    switchMap(() => interval(1000))
  ).subscribe(counter => {
    this.value = counter;
    cd.detectChanges();
  });
}
    

Or make the subscription callback run inside the zone.

      
      constructor(appRef: ApplicationRef, zone: NgZone) {
  appRef.isStable.pipe(
    first(stable => stable),
    switchMap(() => interval(1000))
  ).subscribe(counter => zone.run(() => this.value = counter));
}