Bird
Raised Fist0
Angularframework~5 mins

Query parameters and fragments in Angular - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What are query parameters in Angular routing?
Query parameters are extra bits of information added to the URL after a question mark (?). They help pass data to a route without changing the route path.
Click to reveal answer
beginner
How do you access query parameters in an Angular component?
You use the ActivatedRoute service and subscribe to the queryParamMap observable to get query parameters inside your component.
Click to reveal answer
beginner
What is a URL fragment in Angular routing?
A fragment is the part of the URL after a hash (#). It usually points to a section inside the page, like an anchor link.
Click to reveal answer
intermediate
How do you navigate to a route with query parameters and a fragment in Angular?
Use the Router's navigate method with an object containing queryParams and fragment properties, like: router.navigate(['/path'], { queryParams: { key: 'value' }, fragment: 'section' }).
Click to reveal answer
intermediate
Why should you use query parameters instead of route parameters for optional data?
Query parameters keep the route path clean and stable. They are good for optional or filter data that doesn't affect the main route structure.
Click to reveal answer
Which symbol starts query parameters in a URL?
A&
B?
C#
D/
How do you read query parameters inside an Angular component?
AUsing ActivatedRoute's queryParamMap observable
BUsing Router's navigate method
CUsing window.location.hash
DUsing document.querySelector
What does the fragment part of a URL represent?
AA section inside the page, after #
BThe main route path
CQuery parameters
DThe domain name
Which Angular service helps navigate with query parameters and fragments?
AActivatedRoute
BHttpClient
CRouter
DLocation
Why use query parameters instead of route parameters for optional data?
ABecause route parameters are deprecated
BTo avoid using fragments
CBecause query parameters are faster
DTo keep the route path clean and stable
Explain how to add and read query parameters and fragments in Angular routing.
Think about how URLs have ? for query and # for fragments.
You got /3 concepts.
    Describe when and why you would use query parameters versus route parameters in Angular.
    Consider how the URL looks and what changes the route path.
    You got /3 concepts.

      Practice

      (1/5)
      1. In Angular, what is the main purpose of query parameters in a URL?
      easy
      A. To style the page dynamically
      B. To define the main route path
      C. To specify the component to load
      D. To pass extra information to the route without changing the path

      Solution

      1. Step 1: Understand URL structure

        Query parameters come after a question mark (?) in URLs and carry extra data.
      2. Step 2: Role in Angular routing

        Angular uses query parameters to pass optional info without changing the main route path.
      3. Final Answer:

        To pass extra information to the route without changing the path -> Option D
      4. Quick Check:

        Query parameters = extra info in URL [OK]
      Hint: Query parameters come after ? and don't change the route path [OK]
      Common Mistakes:
      • Confusing query parameters with route parameters
      • Thinking query parameters change the route path
      • Mixing fragments with query parameters
      2. Which Angular Router method is used to navigate to a route with query parameters?
      easy
      A. router.navigate(['/path'], { queryParams: { key: 'value' } })
      B. router.go(['/path'], { params: { key: 'value' } })
      C. router.route(['/path'], { query: { key: 'value' } })
      D. router.change(['/path'], { queryParams: { key: 'value' } })

      Solution

      1. Step 1: Recall Angular Router navigation syntax

        The correct method is navigate with an array for the path and an options object.
      2. Step 2: Identify correct query parameter option

        The option to pass query parameters is queryParams, not params or query.
      3. Final Answer:

        router.navigate(['/path'], { queryParams: { key: 'value' } }) -> Option A
      4. Quick Check:

        Use navigate() with queryParams [OK]
      Hint: Use navigate() with queryParams option to add query parameters [OK]
      Common Mistakes:
      • Using wrong method like go() or change()
      • Passing query parameters as params or query instead of queryParams
      • Not wrapping path in an array
      3. Given this Angular code snippet, what will be logged?
      this.route.queryParams.subscribe(params => console.log(params['page']))
      this.router.navigate(['/items'], { queryParams: { page: 3 } })
      medium
      A. Error
      B. 3
      C. null
      D. undefined

      Solution

      1. Step 1: Understand queryParams subscription

        The subscription listens for query parameter changes and logs the 'page' value.
      2. Step 2: Navigate with queryParams

        The navigate call sets queryParams with page: 3, triggering the subscription.
      3. Final Answer:

        3 -> Option B
      4. Quick Check:

        queryParams['page'] logs 3 after navigation [OK]
      Hint: queryParams subscription logs updated values after navigate [OK]
      Common Mistakes:
      • Expecting undefined because of timing
      • Confusing queryParams with route params
      • Not subscribing to queryParams observable
      4. What is wrong with this Angular code to read the URL fragment?
      this.route.fragment.subscribe(fragment => {
        console.log(fragment.toUpperCase())
      })
      medium
      A. subscribe() is not a function on fragment
      B. fragment is always empty string, so toUpperCase() does nothing
      C. fragment can be null, so calling toUpperCase() may cause error
      D. You must use queryParams to get fragment

      Solution

      1. Step 1: Understand fragment observable behavior

        The fragment can be null if no fragment exists in the URL.
      2. Step 2: Identify risk of calling toUpperCase()

        Calling toUpperCase() on null causes a runtime error.
      3. Final Answer:

        fragment can be null, so calling toUpperCase() may cause error -> Option C
      4. Quick Check:

        Check for null before using string methods on fragment [OK]
      Hint: Check fragment for null before string methods [OK]
      Common Mistakes:
      • Assuming fragment is always a string
      • Trying to get fragment from queryParams
      • Not subscribing to fragment observable
      5. You want to navigate to '/profile' with query parameters { tab: 'settings' } and fragment 'section2'. Which code correctly does this in Angular?
      hard
      A. this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, fragment: 'section2' })
      B. this.router.navigate(['/profile'], { params: { tab: 'settings' }, hash: 'section2' })
      C. this.router.navigate(['/profile'], { query: { tab: 'settings' }, fragment: 'section2' })
      D. this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, hash: 'section2' })

      Solution

      1. Step 1: Recall correct options for navigate()

        Angular uses queryParams for query parameters and fragment for URL fragment.
      2. Step 2: Check option correctness

        this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, fragment: 'section2' }) uses correct keys: queryParams and fragment. Others use incorrect keys like params, query, or hash.
      3. Final Answer:

        this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, fragment: 'section2' }) -> Option A
      4. Quick Check:

        Use queryParams and fragment keys in navigate() [OK]
      Hint: Use queryParams and fragment keys in navigate() options [OK]
      Common Mistakes:
      • Using params or query instead of queryParams
      • Using hash instead of fragment
      • Not passing path as array
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard