填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

TemplateRef

表示一個內嵌範本,它可用於實例化內嵌的檢視。 要想根據範本實例化內嵌的檢視,請使用 ViewContainerRefcreateEmbeddedView() 方法。

Represents an embedded template that can be used to instantiate embedded views. To instantiate embedded views based on a template, use the ViewContainerRef method createEmbeddedView().

檢視"說明"...

      
      abstract class TemplateRef<C> {
  abstract elementRef: ElementRef
  abstract createEmbeddedView(context: C): EmbeddedViewRef<C>
}
    

參見

說明

透過把一個指令放在 <ng-template> 元素(或一個帶 * 字首的指令)上,可以訪問 TemplateRef 的實例。 內嵌檢視的 TemplateRef 實例會以 TemplateRef 作為令牌,注入到該指令的建構函式中。

Access a TemplateRef instance by placing a directive on an <ng-template> element (or directive prefixed with *). The TemplateRef for the embedded view is injected into the constructor of the directive, using the TemplateRef token.

你還可以使用 Query 來找出與某個元件或指令相關的 TemplateRef

You can also use a Query to find a TemplateRef associated with a component or a directive.

屬性

屬性說明
abstract elementRef: ElementRef唯讀

內嵌檢視在其所屬檢視中的位置。

The anchor element in the parent view for this embedded view.

對於從這個 TemplateRef 建立的內嵌檢視,其資料繫結和依賴注入的上下文是從當前位置的上下文中繼承而來的。

The data-binding and injection contexts of embedded views created from this TemplateRef inherit from the contexts of this location.

通常,新的內嵌檢視會被附加到當前位置的檢視容器中,但是在一些高階用例中,該檢視可能被附加到別的容器中, 同時還保留原位置的資料繫結和依賴注入上下文。

Typically new embedded views are attached to the view container of this location, but in advanced use-cases, the view can be attached to a different container while keeping the data-binding and injection context from the original location.

方法

建立一個檢視物件,並把它附著到父檢視的檢視容器上。

Instantiates an embedded view based on this template, and attaches it to the view container.

      
      abstract createEmbeddedView(context: C): EmbeddedViewRef<C>
    
引數
context C

這個新檢視的上下文環境,繼承自所附著的元素。

The data-binding context of the embedded view, as declared in the <ng-template> usage.

返回值

這個新的檢視物件。

EmbeddedViewRef<C>: The new embedded view object.