Creating Angular Modal on ng-click

I'm facing a challenge with a seemingly simple task.

Within an ng-repeat, I have a table of events. My goal is to enable users to click on the event name and view detailed information in a modal window.

To achieve this, I have set up an

ng-click="eventDetails(objectId)"
function linked to the event title. In my controller, the code looks like this:

app.controller('viewEventRequestsController', function($scope, EventFactory){

// Fetching a list of events, each containing an objectId property
        EventFactory.query(function(response){
            $scope.events = response.results;
        });

// Function triggered when clicking an event name            
           $scope.eventDetails = function(objectId){
            $scope.modalOn = true;
             $scope.modal = EventFactory.get({ objectId: objectId });

             console.log($scope.modal)

        }

    });

This snippet demonstrates part of my markup:

<tr ng-repeat="items in events | filter:filter | orderBy:sort:reverse">
        <td>
            <a ng-click="eventDetails(objectId)">{{items.Name}}</a>
        </td>

However, the issue arises as console.log($scope.modal); returns the entire array of objects instead of just the selected one.

In another section of my application, using

$scope.events = EventFactory.get({ objectId: $routeParams.objectId });
works correctly. Unfortunately, this approach doesn't fit my current needs since I must display event details in a modal instead of redirecting to a separate page. Therefore, I cannot employ $routeParams this time.

Are there any suggestions on how to overcome this obstacle?

Answer №1

It seems there may be a mistake in your post, but just to clarify, if your code is as shown, you are passing an object that does not exist into the eventDetails function here:

    <a ng-click="eventDetails(objectId)">

What you should actually pass is:

    <a ng-click="eventDetails(items.itemId /*or whatever the id field is */)">

This is because you are iterating through the events array and assigning each element's value to the items variable here:

    ng-repeat="items in events | filter:filter | orderBy:sort:reverse"

If this is not a typo, then it appears that all Events are being retrieved without applying any filters, similar to calling get() with no filters.

--- UPDATE ---

As you have the Events locally, you can retrieve an individual event without making another server request.

Try updating your eventDetails() function like this:

    $scope.eventDetails = function(event){

       $scope.modalOn = true;
       var index = $scope.events.indexOf(event);
       if(index > -1){
         $scope.modal = $scope.events[index];
       }
    }

In your HTML, simply pass the entire item as an argument to the function:

    <tr ng-repeat="items in events | filter:filter | orderBy:sort:reverse">
    <td>
        <a ng-click="eventDetails(items)">{{items.Name}}</a>
    </td>

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

Understanding conditional statements in JavaScript can greatly enhance your programming skills

Here's a search component that I've been working on. I'm trying to figure out how to handle the scenario where there are no items in the array. Should I use an if statement or is there another approach I should take? Any help would be greatl ...

I encountered a "TypeError: Unable to access property 'name' of undefined" error when attempting to export a redux reducer

UPDATE: I encountered an issue where the namePlaceholder constant was returning undefined even though I was dispatching actions correctly. When attempting to export my selector function, I received an error: Here is the component code: import React, { ...

Applying Angular to Add a CSS Class

I am working with an Angular controller and have the following model on the scope: $scope.model = { subscriber: { email: '', name: '' }, notice: { text: '', type: '' } } My goal is to display a P tag when notic ...

What is the best way to dynamically configure an animation with ng-view in AngularJS?

Currently, I am implementing an ng-view to switch between views with animation effects (angular 1.2RC1). In order to determine the direction of the animation, I add either a slide-left or slide-right class to the ng-view div before triggering the location ...

Emphasize a checkbox by selecting it when another checkbox is checked

I have a question about checkboxes and highlighting the checkmarks. The issue I am facing is that I have multiple checkboxes with the same ID for different screen resolutions. When I click on the label for "Check 1" it highlights the corresponding checkmar ...

Ways to extract specific data from a Json response

Currently, I am engaged in a school project that involves manipulating json data from the Google Geocoding API. I am facing a dilemma on how to properly store the "formatted_address" (as shown below) from the API response so that I can utilize this inform ...

Updating Mapped Components with Selected State

One of the components in my project is a mapped component that dynamically displays API data. Each card displayed by this component receives unique props, resulting in cards that look different from one another. An example can be seen below. View Example ...

"Transferring a variable from the parent Layout component to its children components in Next

I am trying to figure out how to effectively pass the variable 'country' from the Layout component to its children without using state management. Basically, I want to drill it down. import { useState, useEffect } from 'react' import La ...

Creating a CSS grid layout with a scrollbar positioned on the left side and content filling from left to right

How can I move the scrollbar to the left while keeping ng-repeat population from left to right? I'm using an ng-repeat to fill a grid that is 3 by X (Width by height). By default, the scroll bar appears on the right side. I want to position it on the ...

Ways for enabling the user to choose the layout option

I am looking to develop a customized reporting system where users can select the specific fields they want to include in the report as well as arrange the layout of these fields. The data for the reports is sourced from a CSV file with numerous columns. Us ...

Creating a bar graph using data from a JSON file in AngularJS with Highcharts

I am working with a highcharts bargraph in an AngularJS environment. The values for the graph are fetched from a JSON file that has the following format: "bargraph": [ { "categories": "['S', 'M', ...

Having trouble formatting an AJAX POST request properly

Despite my best efforts, I continue to encounter the dreaded 500 (Internal Server Errors) every time I try to execute a POST request to https://rates.tradelanes.us/bankaccount/record/create. I suspect it has something to do with the format of my data. Howe ...

Creating a Custom Hot Module Replacement Hook for Vue.js and Webpack

Is there a way to create a hook that triggers when a Vue component is updated using hot module replacement? [HMR] App is up to date. Now I want to execute a specific method after the update. ...

Tips for positioning a chat box at the bottom of a v-card's visible area

My goal is to create a chat app using Vuejs 3 and Vuetify 3, but I'm encountering an issue aligning the chatbox with the v-card component. Instead of being positioned at the bottom of the v-card, the chatbox (green) appears at the bottom of the page. ...

Mastering form reset functionality using jquery

When attempting to register, an error is generated if any field is left blank in the form (from PHP). The input fields that have been filled out are retained using Smarty: {if isset($smarty.post.registratie.naam)} value="{$smarty.post.registratie.naam}"{el ...

What is the best way to access the display property of a DOM element?

<html> <style type="text/css"> a { display: none; } </style> <body> <p id="p"> a paragraph </p> <a href="http://www.google.com" id="a">google</a> &l ...

Retrieving information from a JSON object in Angular using a specific key

After receiving JSON data from the server, I currently have a variable public checkId: any = 54 How can I extract the data corresponding to ID = 54 from the provided JSON below? I am specifically looking to extract the values associated with KEY 54 " ...

How to set a timeout for a socket.io connection in a node.js application

After searching through the documentation, I couldn't find a specific solution for expiring or disconnecting a socket.io client after a certain period of time. I am seeking a workaround that is both manageable and asynchronous in node.js. One possibl ...

Why does my Redux callback keep getting invoked multiple times?

In developing a react application with redux, I have chosen to avoid using react-redux by manually handling all dispatched events. Below is a sample code snippet. The content of index.html <!DOCTYPE html> <html> <head> <script src=& ...

Messages are not being received despite no errors occurring in the PHP form

I am facing a challenge with a PHP script that is supposed to send a form, but it keeps saying the email has been sent while nothing actually shows up. Do you have any insights on what might be causing this issue? Also, there's nothing in the spam fol ...