2017-08-29 14 views
0

私はこれを持っている:router.urlに特定の文字列が含まれているかどうかを確認しますか?

if (this.router.url === '/test/sort') { 

        this.active = 0; 
       } 

問題は、時にはURLがtest/sort?procesId=11とif文、それはで入らないよりになるということです。どのように私はそれを行うことができますどのような提案?

+2

質問のタイトルをコピーしてGoogleに貼り付け、結果の一部を読んでみませんか? [最初のもの](https://stackoverflow.com/questions/32133680/how-to-check-whether-the-url-contains-id-or-not)のように、あなたが望むものはかなり正確です。 – musefan

+0

私はidが必ずしもありません – None

答えて

4

あり、このためにはるかに良い/クリーナーのソリューションがありますが、あなたが働く基本的な何かをしたい場合:

if (this.router.url.indexOf('/test/sort') > -1) { 
    this.active = 0; 
} 
+0

私はあなたがそれが良い答えではないと言っているが、はるかに良い解決策があるという根拠に基づいてこの答えをdownvoteするように誘惑されています... – musefan

0

をあなたはパラメータなしでURLを取得するためにLocationStrategyを使用することができます。

import {LocationStrategy} from '@angular/common'; 


export class AppComponent implements OnInit { 

    constructor(private url:LocationStrategy) { } 

    ngOnInit() { 

     console.log(this.url.path()); 
     if(this.url.path()==='/test/sort'){ 

     this.active=0; 
     } 

    } 

} 
関連する問題