## ViewContainerRef
表示可以附加一個或多個視圖的容器。
容器可以包含兩種視圖。通過`createComponent`實例化組件創建的宿主視圖(Host View)和通過`createEmbeddedView`實例化嵌入模板創建的嵌入視圖。
在包含視圖內的視圖容器的位置由錨點元素指定。每個視圖容器只能有一個錨點元素,每個錨點元素只能有一個視圖容器。
附加到此容器的視圖的根元素將成為渲染視圖中錨點元素的兄弟。
要訪問元素的`ViewContainerRef`,您可以放置一個在元素上使用`ViewContainerRef`注入的指令,或者通過`ViewChild`查詢獲取它。
### 類定義
```typescript
class ViewContainerRef {
element : ElementRef
injector : Injector
parentInjector : Injector
clear() : void
get(index: number) : ViewRef
length : number
createEmbeddedView(templateRef: TemplateRef<C>, context?: C, index?: number) : EmbeddedViewRef<C>
createComponent(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][]) : ComponentRef<C>
insert(viewRef: ViewRef, index?: number) : ViewRef
move(viewRef: ViewRef, currentIndex: number) : ViewRef
indexOf(viewRef: ViewRef) : number
remove(index?: number) : void
detach(index?: number) : ViewRef
}
```
### 屬性
- element: `ElementRef`
描點元素,用于指定此容器在包含視圖中的位置。
- injector : `Injector`
- parentInjector: `Injector`
- clear() : `void` 銷毀容器中的所有視圖
- get(index: `number`) : `ViewRef` 返回此容器中指定索引的視圖所對應的`ViewRef`。
- length: `number` 返回當前附加到此容器的視圖數。
- createEmbeddedView(templateRef: `TemplateRef<C>`, context?: C, index?: `number`) : `EmbeddedViewRef<C>`
基于`templateRef`實例化嵌入視圖并將其插入到當前容器的指定索引處。
如果未指定`index`,則新視圖將作為為容器的最后一個視圖插入。
返回新創建視圖的`ViewRef`對象。
- createComponent(componentFactory: `ComponentFactory<C>`, index?: `number`, injector?: `Injector`, projectableNodes?: any[][] : `ComponentRef<C>`)
實例化單個組件并將其宿主視圖(Host View)插入此容器指定的索引處。
組件使用`ComponentFactory`實例化,并通過`ComponentFactoryResolver`獲取。
如果未指定`index`,則新視圖將作為為容器的最后一個視圖插入。
可以選擇指定`Injector`作用于當前組件的父節點。
返回新實例化組件的Host View的`ComponentRef`。
- insert(viewRef: `ViewRef`, index?: `number`) : `ViewRef`
在容器指定索引處插入一個被`ViewRef`標識的視圖。
如果未指定`index`,則新視圖將作為為容器的最后一個視圖插入。
- move(viewRef: `ViewRef`, currentIndex: `number`) : `ViewRef`
將`ViewRef`標識的視圖移動到容器指定`index`處。
- indexOf(viewRef: `ViewRef`) : `number`
返回在當前容器中通過ViewRef指定的視圖的索引,如果此容器不包含視圖,則返回-1。
- remove(index?: `number`) : `void`
銷毀指定索引處附加到此容器的視圖。
如果未指定index,容器中的最后一個視圖將被刪除。
- detach(index?: `number`): `ViewRef`
與`insert`一起使用以在當前容器中移動視圖。
如果省略索引參數,則會刪除最后一個ViewRef。
- 說明
- angular 1.x
- ngModelController
- ngOptions
- ngModelOptions
- lifecycle
- directive
- angular 2
- @angular/forms
- 類
- AbstractControl
- AbstractControlDirective
- AbstractFormGroupDirective
- FormControl
- FormArray
- FormBuilder
- FormGroup
- NgControl
- 接口
- controlValueAccessor
- 指令
- DefaultValueAccessor
- Angular 2 生命周期
- OnInit
- DoCheck
- @angular/router
- 配置
- Routes
- 指令
- RouterOutlet
- RouterLink
- 接口
- ActivatedRoute
- UrlTree
- NavigationExtras
- ActivatedRouteSnapshot
- RouterStateSnapshot
- 類
- UrlSegment
- UrlSegmentGroup
- UrlSerializer
- DefaultUrlSerializer
- Router
- bug記得
- @angular/http
- 類
- Http
- Body
- Response
- ResponseOptions
- Header
- Request
- RequestOptions
- URLSearchParams
- @angular/core
- decorator
- Component-decorator
- animation
- DI
- linker
- TemplateRef
- ElementRef
- EmbeddedViewRef
- ViewRef
- ViewContainerRef
- Query
- ComponentFactory
- ComponentRef
- Renderer
- change_detection
- KeyValueDiffers
- IterableDiffers
- ChangeDetectorRef
- ChangeDetectionStrategy
- Zone
- ngZone
- @angular/common
- 指令
- NgTemplateOutlet
- QueryList
- bootstrap4
- card
- form
- 重點關注博客
- 學習過的文章
- 筆記
- Angular 2 雙向綁定
- 將字符串解析成DOM
- rx相關
- operators
- combineLatest
- combineAll
- concat(All, Map, *MapTo)
- 背壓(backpressure)
- js事件keycode對應表
- 裝飾器
- 有用的代碼摘錄
- 日期操作
- 數量操作
- 字符操作
- rxjs問題
- 小示例
- h5面試準備
- react
- 開發遇到的問題