首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular2,如何在viewContianer中查找视图的索引

Angular2,如何在viewContianer中查找视图的索引
EN

Stack Overflow用户
提问于 2016-09-18 08:19:45
回答 2查看 2.2K关注 0票数 3

假设使用我有一个指令,可以在它的viewContianerRef中动态加载组件:

代码语言:javascript
运行
复制
@Directive( { selector : '[loader-ref]' } )
export class LoaderDirective {
    constructor ( private cd : ChangeDetectorRef ,
                  private viewContainer : ViewContainerRef ,
                  private componentResolver : ComponentResolver ) {
    }
    getIndexOf(viewRef:ViewRef){

       return this.viewContainer.indexOf(viewRef);
    }
    createComponent ( dialogComponent : { new() : any } ) : Promise<ComponentRef<any>> {
        return this.componentResolver
                   .resolveComponent( dialogComponent )
                   .then( ( componentFactory : ComponentFactory<any> ) => {
                       return this.viewContainer.createComponent( componentFactory );
                   } );
    }
}

我应该动态加载的组件:

代码语言:javascript
运行
复制
@Component({
  selector:'my-dynamic-component'
})
export class myDynamicComponent{

   // Is there any way to get this component's ViewRef , or HostView? 


}

我使用LoaderDirective动态加载一个组件,如下所示:

我的应用:

代码语言:javascript
运行
复制
@Component( {
    selector   : 'example' ,
    template:` <button (click)='getIndex()'></button> <div loader-ref></div>`
})
export class ExampleComponent {
    @ViewChild( LoaderDirective ) loaderDirective : LoaderDirective;


    ngOnInit(){
         let waitForChunk = require( 'myDynamicComponent.ts' );
    waitForChunk( ( file ) => {
        this.loaderDirective
            .createComponent( file[ 'default' ] )
            .then( ( componentRef : ComponentRef<any> ) => {
                componentRef.changeDetectorRef.detectChanges();
            } );
    } );

    }


    getIndex(){
         // Here I want to get index of myDynamicComponent which is loaded 
        ////What do I need to do ?   ??
         let index = this.loaderDirective.getIndexOf(what to pass ? )

         console.log('My index is : '+index);

    }
}

这是有效的,但请记住我的问题:

在我的指令中,我有一个viewContainerRef,它有一个名为indexOf的方法。

此方法应该返回viewContainerRef中已加载组件的索引,但我不知道它是如何工作的以及如何使用它:

EN

Stack Overflow用户

发布于 2016-09-18 14:18:09

我想您正在寻找ComponentRefhostView属性

代码语言:javascript
运行
复制
loadedView: ViewRef;
...
  this.loaderDirective
    .createComponent(myDynamicComponent)
    .then((componentRef: ComponentRef<any>) => {
      componentRef.changeDetectorRef.detectChanges();
      this.loadedView = componentRef.hostView; <== this line
    });
...
getIndex(){
  let index = this.loaderDirective.getIndexOf(this.loadedView);
  console.log('My index is : '+index);
}

但在您的情况下,它将始终等于0,因为您正在触发:

代码语言:javascript
运行
复制
this.viewContainer.clear();

另请参阅angular2 2.0.0

Update1

如果您希望访问myDynamicComponent指令中的视图,则可以利用以下内容:

代码语言:javascript
运行
复制
@Component({
  selector: 'my-dynamic-component'
})
export class myDynamicComponent {
  public view: ViewRef;
}

...
 this.loaderDirective
    .createComponent(myDynamicComponent)
    .then((componentRef: ComponentRef<any>) => {
      componentRef.changeDetectorRef.detectChanges();
      componentRef.instance.view = componentRef.hostView; <== add this line
    });

我更新了

Update2

您也可以通过以下方式获取viewRef:

代码语言:javascript
运行
复制
constructor(private injector: Injector) {}

...
var viewRef = this.injector._view.ref;

但在本例中,您将使用Injector的私有属性。这是一种糟糕的做法。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39552953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档