Is there a way to modify the appearance of a particular element in ng-repeat?

On my website, I am using ng-repeat to display various dish postings. I want to trigger a modal to open when I click on a dish, showing the specific data for that dish. To achieve this, I'm assigning each dish a modal div with a unique ID (dish-$index) and toggling its display style from none to block. However, I'm unable to select the element by its ID dish-{{$index}} in my JavaScript code, as it returns null. Why is this happening and what is the best alternative approach? I prefer not to utilize bootstrap for this.

        <!-- Trigger/Open The Modal -->
        <button class="order">Open Modal</button>

        <div id="dish-{{$index}}" class="modal4">

          <!-- Modal content -->
          <div class="modal-content4">
            {{dish.dishName}}

            <span class="close4">x</span>
          </div>

        </div>
    </div>

var selector = 'div[ng-id="' + child.key + '"]';
var modal4 = document.getElementById("dish-{{$index}}");
var span4 = document.getElementsByClassName("close4")[0];

$('body').on('click', selector, function () {
     google.maps.event.trigger(marker, 'click', {});
     modal4.style.display = "block";
                });

Answer №1

Attempting to utilize Angular interpolation within JavaScript using

document.getElementById("dish-{{$index}}");
will prove unsuccessful.

Instead of attaching an on-click event with jQuery, consider utilizing the Angular directive ng-click. Assuming your dishes are stored in an array of JavaScript objects.

<div ng-repeat="dish in dishes">
   <button class="order" ng-click="dish.modalVisible = true">Open Modal</button>

    <div id="dish-{{$index}}" class="modal4">

      <!-- Modal content -->
      <div class="modal-content4" ng-if="dish.visible">
        {{dish.dishName}}

        <span class="close4" ng-click="dish.modalVisible = false">x</span>
      </div>
</div>

The ng-if directive will only insert the HTML element into the DOM when the condition evaluates to true. By using the first ng-click, the modalVisible property is set to true and the modal is rendered. Clicking the close button with ng-click will set modalVisible to false, removing the modal from the DOM. Alternatively, ng-show can be used, which hides the element while still in the DOM.

Refer to the Angular website documentation for more information:

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

Dealing with audio bleed from a previous recording using fluent-ffmpeg

const Discord = require('discord.js'); const client = new Discord.Client(); const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg'); const ffmpeg = require('fluent-ffmpeg'); ffmpeg.setFfmpegPath(ffmpegInstaller.path); co ...

Arranging data in a JSON array while handling null values in JavaScript

Here is my JSON array: var jData = [ {id: 1, parent: null}, {id: 2, parent: null}, {id: 3, parent: 1}, {id: 4, parent: 2}, {id: 5, parent: 2}, {id: 6, parent: 1}]; I would like to sort it in the following order (first by id then by parent): [ {id: 1 ...

"Using the Google Maps directive inside a separate modal directive in Angular results in a blank map display

As a newcomer to Angular, I have encountered a hurdle while attempting to incorporate a 'google maps' directive inside another directive. The following code showcases a 'modal-view' directive that loads a form: angular.module(&apo ...

Issue with Express.js res.append function: Headers cannot be set after they have already been sent

I encountered an issue in my express project where I tried to set multiple cookies using "res.append" in the same request, but I kept getting an error saying "Error: Can't set headers after they are sent.". Can someone help me identify the problem and ...

Ways to insert data in angularjs

As I attempt to add data to a list using the addGroup function, I encounter a type-error. The error message reads: "TypeError: Cannot read property 'push' of undefined" at r.$scope.addGroup (main.js:7) at fn (eval at compile (angular ...

Using the jQuery .submit() function in conjunction with Angular

Is there a way to submit a form using an Angular controller? Also, how can we invoke jQuery functions within Angular? <form id="flightRules" target="_blank" action="<?= Yii::$app->request->baseUrl ?>/site/farerules" method="post"> ...

Accurate function calls with multiple parameters in TypeScript

As a beginner in TypeScript and currently exploring its integration with AngularJS, I am facing a particular issue where the compiler is not detecting an error. In Angular, a resource provider typically includes a get() method that returns an instance of ...

Keep things in line with async functions in Node JS

Hello, I am just starting out with NodeJs and I have a question. I am trying to push elements into an array called files based on the order of the urls provided, but it seems like I'm getting a random order instead. Below is the code I've been wo ...

Error message: "Issue encountered with locating Node import module while operating within a docker

I've created a React app along with a Node.js server that includes the following imports: import express from 'express' import compression from 'compression' import cookieParser from 'cookie-parser' import bodyParser from ...

Adjust the size of the Threejs canvas to fit the container dimensions

Is there a way to determine the canvas size based on its container in order to prevent scrolling? Setting the size based on the window results in the canvas being too large. ...

The 'in' operand is invalid

I am encountering a JavaScript error: "[object Object]" TypeError: invalid 'in' operand a whenever I attempt to perform an AJAX request using the following code: .data("ui-autocomplete")._renderItem = function( ul, item ) { return $( " ...

Posting data using the form method="post" in Vue does not seem to be working

My goal is to create a website that allows users to reserve a seat using a specific password and time slot. I've set up a form where users can input the password and their name, then submit the form to make the reservation. However, I'm facing an ...

Include the data-title attribute within the <td> elements of my datatables

To apply the magic of jQuery datatables to all my tables, I use datatables. For responsive tables, I add data-title to my td's. Is there a way to automatically add data-title to all my td's like below? <td data-title="Fruit">Apple</td&g ...

limit mongoose search results to a specific year

Would it be possible to add an option for the api user to filter the wine query by year? However, if no year is specified, mongoose should not return an empty array. The same applies to the price property. For example, http://localhost:1234/api/wine?year= ...

What are the best ways to personalize my code using Angular Devextreme?

Our development team is currently utilizing Angular for the front-end framework and ASP.net for the back-end framework. They are also incorporating Devextreme and Devexpress as libraries, or similar tools. However, there seems to be difficulty in implement ...

Having trouble launching the Nuxt 3 development server because the module '@volar/typescript/lib/node/proxyCreateProgram' cannot be located

Whenever I attempt to execute npm run dev, the dev server initiates but encounters the following error: Cannot start nuxt: Cannot find module '@volar/typescript/lib/node/proxyCreateProgram Require stack: - node_modules\vite-plugin-checker&bsol ...

Musical backdrop for an online game platform

Currently, I am working on developing a game using HTML. I am trying to figure out the best way to incorporate music into the gameplay. Any suggestions on how I can add music to my web-based game? ...

Transfering data to Handlebars while rendering a template

Is there a method to pass a variable to a handlebars template during its rendering process? Below is the template in question: <script id="listingTopics" type="text/x-handlebars-template"> {{#each this}} <div class="wrapper_individual_listing ...

coming back to the cordova application after using the browser

I recently developed a basic app using the ionic framework. There is a feature in the app that opens an external payment gateway in a system browser to process payments. After successful payment, a script on my server sends an HTML page to display a succes ...

list of key combinations in a ul element

I programmed a list of world states in PHP using an unordered list (<ul>): $WORLD_STATES = array( "France", "Germany", "Greece", "Greenland", "United Kingdom", "United States", "Uruguay" ...