从角网中获取ID

时间:2018-04-07 03:01:01

标签: javascript angular

我试图从图像中获取第一个ID但是我确实在分​​号后面接收了id。 (" id" = 1)。根据我的代码,我不知道如何解决这个问题。有什么建议。我真的不知道如何执行这项任务。我试过看不同的资源而没有运气。

" http://localhost:4200/image/1;id=1;catergory=testing"



  ngOnInit() {
        const id$ = this.route.paramMap.pipe(map((params) => params.get('id') || 0), map(n => Number(n)));
        id$.subscribe(id => {
          this.activeId = id;
          console.log("ActiveId",this.activeId);
          this.testing =  this.graphService.getTestingGraphs(id);
          this.image = this.imageService.getImage(id);
        });
          this.route.params
          .subscribe(params => {
          const id = +params['id'];
          console.log(`Current param ID is: ${id}`);
    });

}




1 个答案:

答案 0 :(得分:0)

订阅路线事件,以便具有相同网址段的路线中的任何更改都会自动反映。

使用[window.location.pathname],因为param的返回值始终是{id:' 1',category:' testing' }

试试这个

this.route.params.subscribe(response => {
     //Base in your url "http://localhost:4200/image/1;id=1;catergory=testing"
     //when we access window.location.pathname we get '/image/1;id=1;catergory=testing'

      let path = window.location.pathname.split('/'); // <- output would be ['', 'image', '1;id=1;catergory=testing']
      let myParams = path[2].split(';'); // <- output would be ["1", "id=1", "catergory=testing"]

    //So to get your id
      let id = myParams[0];
});