Angular 2 @ViewChild returns undefined

后端 未结 6 1223
南旧
南旧 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:23

    Another possible scenario is when you are using typescript in angular or ionic framework and calling the ViewChild element from a function having javascript like signature. Please use a typescript like function signature instead (e.g. using fat arrow =>). For example

    accessViewChild(){
        console.log(this.galleryContainer.nativeElement.innerHTML);
    }
    

    would show print undefined however

    accessViewChild = ()=>{
        console.log(this.galleryContainer.nativeElement.innerHTML);
    }
    

    would print the inner html.

提交回复
热议问题