Angular 2 @ViewChild returns undefined

后端 未结 6 1217
南旧
南旧 2020-12-15 02:57

I\'ve looked at several related posts and documentation, but still can\'t seem to get expected behavior from @ViewChild.

Ultimately, I\'m trying to set the scroll p

6条回答
  •  一整个雨季
    2020-12-15 03:22

    Try using a ref in your template instead:

    
    

    And use the ref name as the argument:

    @ViewChild('galleryContainer') galleryContainer: ElementRef;
    

    EDIT

    Forgot to mention that any view child thus declared is only available after the view is initialized. The first time this happens is in ngAfterViewInit (import and implement the AfterViewInit interface).

    The ref name must not contain dashes or this will not work

提交回复
热议问题