Navigating through AngularJS from main page to detailed page with dynamic routing

I need assistance with routing from a list of items in my index to a detailed view page for each item.

Within my index view, there is a table that displays all the saved items from the database.

Under the actions column, there is a button that should direct me to the events/show route using the ng-click="go('events/show')"

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Title</th>
            <th class="col-md-2">Actions</th>
        </tr>
    </thead>
    <tbody>
        <tr scope="row" ng-repeat="event in events | reverse | filter:filterByUID">
            <td>{{event.title}}</td>
            <td class="col-md-2">
                <div class="btn-group" role="group" aria-label="actions">
                    <button class="btn btn-primary" ng-click="go('events/show')">
                        <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
                    </button>
                    <button class="btn btn-primary" ng-click="events.$remove(event)">
                        <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                    </button>
                </div>
            </td>
        </tr>
    </tbody>
</table>

The layout of the table can be seen here: https://i.sstatic.net/rC8Ra.png

In my controller, I've included:

$scope.go = function ( path ) {
      $location.path( path );
    };

Also, in my routes.js file, the setup is as follows:

.whenAuthenticated('/events/show', {
        templateUrl: 'views/eventShow.html',
        controller: 'eventShowCtrl'
      })

At this stage, everything is functioning correctly.

However, I'm unsure about how to pass the event ID to the eventShow.html page so that I can identify which item was clicked in the index list and display its details?

This is how my firebase database appears:

https://i.sstatic.net/GgGhu.png

Answer №1

Have you tried using ui-router for more seamless dynamic routing?

Check out ui-router on GitHub

If you prefer to stick with your current setup, consider passing the event id in your path like this

$scope.go = function ( path, event ) {
  $location.path( path + "/" + event.id );
};

.whenAuthenticated('/events/show/:eventId', {
    templateUrl: 'views/eventShow.html',
    controller: 'eventShowCtrl'
 })

Then, in your controller, you can retrieve the event by accessing $stateParams.eventId.

Answer №2

It's important to incorporate a variable into your router:

.whenAuthenticated('/events/:id', {
    templateUrl: 'views/eventShow.html',
    controller: 'eventShowCtrl'
})

This way, you can easily reference the ID in your function call:

go('events/:id')

If you're looking for guidance, check out this informative tutorial (I suggest watching both parts).

By following these steps, you'll be able to create more user-friendly URLs that are also bookmarkable.

Answer №3

If you want to pass the UID (user id) onClick

<tr scope="row" ng-repeat="event in events | reverse | filter:filterByUID">
  <td>{{event.title}}</td>
  <td class="col-md-2">
    <div class="btn-group" role="group" aria-label="actions">
      <button class="btn btn-primary" ng-click="go('events/show', event.UID)">
         <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
      </button>
      <button class="btn btn-primary" ng-click="events.$remove(event)">
         <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
      </button>
    </div>
  </td>
</tr>

In your JavaScript file, you can define the function like this:

$scope.go = function ( path, uid ) {
  $location.path( path + "/" + uid );
};

.whenAuthenticated('/events/show/:eventId', {
    templateUrl: 'views/eventShow.html',
    controller: 'eventShowCtrl'
 })

When querying Firebase, if you have a field called uid in your objects, you can use the startAt and endAt methods.

Check out this example on JSFiddle

For more information on filtering data in Firebase, you can read this documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error in Postman: Express and Mongoose throwing 'name' property as undefined

While trying to create and insert 'user' JSON documents according to the model below, upon sending a POST request to localhost:3000/api/student, I encountered an error using Postman: TypeError: Cannot read property 'name' of undefined ...

What is the best way to add attachments to the clipboard in a Chrome extension?

One possible way to achieve this is by using the navigator.clipboard.write API, but keep in mind that this API is not available to background pages of Chrome extensions. A method I attempted involved creating a blob like this: let blobFinal = null; // ...

Verify the identity of all REST API requests without the need for a username or password in order to obtain a

I have a unique setup where I am selling products. The API fetches product data from a centralized node back-end and displays it on an angular front-end that is hosted on multiple domains. The challenge I'm facing is the need to authenticate all reque ...

Perform MySQL commands on demand using Node.js

As I dive into learning Node.js, I've been working on a program that involves checking user credentials against a MySQL database. However, I'm not entirely sure if my authentication process is correct. My main query revolves around the ability to ...

Updating a field based on user input is a common operation in software development. Learn how

Below is the code used to create a field: <tr><td> <input type="text" value="good" onfocus="if (this.value == 'good') {this.value=''}" onblur="if(this.value == '') { this.value='good'}" name="name" m ...

Is there a way to retrieve the value of a select element that has more than one option selected?

On my website, there is a select element that enables users to choose multiple options. My goal is to gather an array containing the values of all currently selected options. I attempted to utilize the selectedOptions property of the select element in ord ...

Guide to integrating HTML templates with Vue.js

I am experimenting with using vue.js but encountered some issues when trying to integrate it with a purchased template. I have included all the necessary .css and .js files in my index.html file and then called the components within vue.js itself. index.h ...

Countdown to redirect or exit on Jquery mobile "pageshow" and "pagehide" events

Looking to implement a 30-second countdown on a Jquery Mobile page with specific requirements: (1) Countdown begins on pageshow (2) Redirects to new page when countdown expires (3) If user navigates away (pagehide) before countdown finishes, the timer fun ...

Steps for disabling a <select> <option> when a different item is selected in another <select><option> using Angular JS

I am working with two <select> <option> elements: <select name="fm" id="fm" class="form-control" > <option value="01" >All</option> <option value="01">01</option> <option value="02">02</optio ...

AngularJS - returning null from factory after refreshing the page

I am facing an issue with two controllers, two views, and a factory in my AngularJS application. I successfully set a property in the factory from the first controller and retrieve it in the second controller. However, when I refresh the page (view 2), the ...

Internet Explorer does not offer support for stopping scrolling using either jQuery or CSS

I have created an overlapping screen similar to Facebook's photo viewer, and it works well on Chrome, Safari, and Firefox. However, I am struggling to make it work on Internet Explorer (IE). The issue is that the div overlaps correctly but allows scro ...

Implementing Jquery tabs into the code causes the vertical auto-scroll to smoothly glide beyond anchor points

I recently implemented a smooth autoscroll feature on my webpage using the CSS-Tricks code found here: http://css-tricks.com/snippets/jquery/smooth-scrolling/ Everything was working perfectly until I added jQuery tabs to display some of the content. Now, ...

Is it better to import and use useState and useEffect, or is it acceptable to utilize React.useState and React.useEffect instead?

When I'm implementing hooks for state, effect, context, etc, this is my usual approach: import React, { useState, useEffect, useContext } from 'react'; However, I recently discovered that the following also works perfectly fine: import Re ...

Modifying a table row in real-time with the power of JQuery/JavaScript

I successfully created a table with a for loop in Java Spring and now I'm trying to dynamically show and hide specific parts of it when a button is clicked. Here's a simplified version of what I have: <jsp:attribute name= "scripts"> ...

Creating a dynamic Three.js scene based on a variable

Having limited knowledge in html/javascript, I decided to start with three.js. My scene relies on a parameter that users can manipulate. Below is a simple example of the scene setup. It showcases a parametric surface that adjusts based on a user-controlle ...

What is the best way to display the adjacent photos surrounding the one currently selected?

I am attempting to create an image slider that allows users to choose an image from a list of images. The slider displays three images at a time in three separate divs: "image-center," "image-left," and "image-right." The center div works correctly, but th ...

Not quite sure why the commitment is not being fulfilled

Before in my angular controller, I had: var getResponse = function () { $http.post("api/screen/", { Key: 'banana', value: null }) .then(onComplete, onError); }; var onError = function (reason) { $scope.error = "F ...

What could be causing my concentration to be so off?

When I load the data.json file after focusing, my datepicker does not show up. This puzzles me because it appears on the second focus attempt. Where could I be going wrong? function flattenFieldsArray(arr) { return arr.map(function(item) { ...

What is the process for creating React Native components for various build types and flavors?

When working with React Native, there is a useful feature that allows us to use different component files based on the platform. For example, using component.ios.js and component.android.js will generate a component module with specific behavior for iOS an ...

How can we create lists using parentheses specifically with Javascript or jQuery?

I am looking to create a formatted list using <ol> and <ul> tags with parentheses included. (1) List1 (2) List2 (3) List3 (4) List4 The challenge is to achieve this formatting purely with javascript or jQuery, without relying on any external ...