Angular is it possible to detect if query Paramter has changed?

南笙酒味 提交于 2019-12-21 17:47:45

问题


Angular is it possible to detect if query Paramter has changed? I have a Component that handles 2 query paramters and according to what paramter you come with it sets some variables true or false. The Problem is if im on my component with queryparameter 1 the qp can change to the second paramter without leaving the component and in that case i need to set variables diffrently. So how do I detect that? Is that even possible?


回答1:


You can subscribe to the params in the root component

constructor(route:ActivatedRoute) {
  route.queryParams.subscribe(p => console.log(p.myQueryParam)); // you can also do this in ngOnInit
}

See also https://angular.io/api/router/ActivatedRoute

You can have query params on other route levels as well, but then they are called matrix parameters.

See also the end of this section https://angular.io/guide/router#route-parameters-required-or-optional




回答2:


You can subscribe to the params observable provided by ActivatedRoute module to observe changes in the query parameters.

constructor(private route:ActivatedRoute) {
this.route.params.subscribe(
  (params:Params)=>{
    console.log(params['yourId']);
  }
)}


来源:https://stackoverflow.com/questions/48003510/angular-is-it-possible-to-detect-if-query-paramter-has-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!