Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度5无法读取属性';滚动到视图';空的_Angular_Angular5 - Fatal编程技术网

Angular 角度5无法读取属性';滚动到视图';空的

Angular 角度5无法读取属性';滚动到视图';空的,angular,angular5,Angular,Angular5,我想在页面加载时将页面滚动到元素。请参见下面的示例: A) http://localhost:4200/#/test/test-history B) http://localhost:4200/#/test/test-history#orderhistory 当我输入A)时,我希望正常的页面加载没有滚动效果。 当我输入B)时,我希望页面加载并滚动到元素 在HTML中,我做了Bla-Bla 在.ts文件中的ngOnInit()下,我执行了以下操作: this.scroll('orderhisto

我想在页面加载时将页面滚动到元素。请参见下面的示例:

A) http://localhost:4200/#/test/test-history
B) http://localhost:4200/#/test/test-history#orderhistory
当我输入A)时,我希望正常的页面加载没有滚动效果。 当我输入B)时,我希望页面加载并滚动到元素

在HTML中,我做了
Bla-Bla

在.ts文件中的
ngOnInit()
下,我执行了以下操作:

this.scroll('orderhistory');
我在下面写了一个函数:

scroll(id) {
      let el = document.getElementById(id);
      el.scrollIntoView({behavior: 'smooth', block: 'start', inline: 'nearest'});
    }
但是,我面临着错误:

无法读取null的属性“scrollIntoView”

我看到了一些关于这个的帖子,并应用了一些方法,比如
scrollToBottom()但是没有运气

感谢您的帮助


已修改:

ngAfterViewInit() {
      // Scroll (Anchor) Function to Order History
        this.scroll('orderhistory');
    }
以上仍然显示错误

无法读取null的属性“scrollIntoView”


问题是您的
el
尚未渲染。这样做的原因可能是在DOM中不存在元素时调用this.scroll
from
ngOnInit
。您应该在
ngAfterViewInit
中调用
scroll
方法,该方法在视图初始化后只需调用一次

问题是您的
el
尚未渲染。这样做的原因可能是在DOM中不存在元素时调用this.scrollfrom
ngOnInit
。您应该在
ngAfterViewInit
中调用
scroll
方法,该方法在视图初始化后只需调用一次

由于存在显示div的条件,因此当
requestforcostshipping
的值为true时,需要调用滚动函数

ngAfterViewInit() {
      // Scroll (Anchor) Function to Order History
      if(this.requestForCostShipment){
        this.scroll('orderhistory');
       }
    }

或者调用
this.scroll('orderhistory')
其中
requestforcostshipping
的值设置为
true

因为存在显示div的条件,所以当
requestforcostshipping
的值为true时,您需要调用滚动函数

ngAfterViewInit() {
      // Scroll (Anchor) Function to Order History
      if(this.requestForCostShipment){
        this.scroll('orderhistory');
       }
    }

或者调用
this.scroll('orderhistory')其中
requestforcostshipping
的值设置为
true

可能在执行OnInit时元素orderhistory还不存在。可能在执行OnInit时元素orderhistory还不存在。如上所述完成并修改了我的帖子。错误仍然存在。然后发布您的html结构,从何处调用以滚动到此元素,从何处呈现OrderHistory div上是否存在
*ngIf
条件?是的,@hana_wjira我已在我的postDone中编辑html结构,并如上所述修改了我的帖子。错误仍然存在。然后发布您的html结构,从何处调用以滚动到此元素,从何处呈现OrderHistory div上是否存在
*ngIf
条件?是的,@hana_wjira我在帖子中编辑了html结构