Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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
Javascript 重新加载内容_Javascript_Angular_Domcontentloaded - Fatal编程技术网

Javascript 重新加载内容

Javascript 重新加载内容,javascript,angular,domcontentloaded,Javascript,Angular,Domcontentloaded,我了解angular是一个包含多个组件的单页应用程序,并使用route在页面之间进行交互 我们有一个像这样的角度应用程序。其中一个要求是,在特定组件上,我们需要向DomContentLoaded事件添加eventListener 由于index.html页面之前已经加载(因此触发了DomContentLoaded事件),因此出现了一个问题 我找不到重新触发DomContentLoaded事件的方法 我不太清楚,为什么需要听angular中加载的DomContentLoaded,因为那里有很多函数

我了解angular是一个包含多个组件的单页应用程序,并使用route在页面之间进行交互

我们有一个像这样的角度应用程序。其中一个要求是,在特定组件上,我们需要向DomContentLoaded事件添加eventListener

由于index.html页面之前已经加载(因此触发了DomContentLoaded事件),因此出现了一个问题


我找不到重新触发DomContentLoaded事件的方法

我不太清楚,为什么需要听angular中加载的DomContentLoaded,因为那里有很多函数,这取决于您使用的angular版本

export class SomeComponent implements AfterViewInit {
 ngAfterViewInit(): void {
 }
}


但是,您可以使用*ngIf(Angular2+)或ng if(AngularJS)重新触发组件的渲染。只需在那里放置一个布尔值,然后在事件上切换布尔值。

请尽量不要使用此类侦听器,它们(通常)在事件中不需要。尝试使用

在您的情况下,我建议使用,它在组件完成渲染时激发

从'@angular/core'导入{Component,afterview checked};
@组成部分({
...
})
导出类WidgetLgFunnelsComponent在查看检查后实现{
ngAfterViewChecked(){
console.log('Component finished rendering!')
}
}

如果要对组件模板内的DOM元素执行某些操作,并且要确保它们存在,则应使用angular的钩子:

@Component({
  // ...
})
export class SomeComponent implements AfterViewInit {
  ngAfterViewInit(): void {
    // here you can be sure that the template is loaded, and the DOM elements are available
  }
}

在创建第一个组件之前激发的DomContentLoaded事件。但是,您可以使用
ngAfterViewInit()
方法。
ngAfterViewInit()
是一种回调方法,在Angular完成组件视图的初始化后立即调用。当视图被实例化时,它只被调用一次

class YourComponent {
    ngAfterViewInit() {
        // Your code here
    }
}

如果确实需要捕获DomContentLoaded事件,请在main.ts文件中执行

谢谢马库斯。。。以及所有其他回答的人。。我必须重新触发DomContentLoaded事件,因为其中一个服务提要是脚本标记,它将事件列表添加到了DomContentLoaded事件中。我尝试使用ngAfterViewIniti(),但它对我无效。:)