What is the best way to show a specific element within a list item when clicked using AngularJS?

Is there a way to display an iframe element inside an li element on ng-click, but only for the specific one that was clicked? Additionally, how can all iframes be hidden using AngularJs?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function ($scope, $http, $sce) {
        var indx;
        $scope.PlayFun = function (order) {
            indx = order;
            // This function is necessary for executing multiple tasks.
        };
    });
</script>

<div data-ng-app="myApp" data-ng-controller="myCtrl">
    <ul style="list-style-type: none">
        <li data-ng-click="PlayFun($index)" data-ng-repeat="x in records">
            <div data-ng-if="indx === $index">
                <iframe id="video" style="width: 100%; height: 300px;" 
                        data-ng-src="{{videoSource}}" allowfullscreen>
                </iframe>
            </div>
        </li>
    </ul>
</div>

In simpler terms:
I want the PlayFun function to execute when a list item (li) is clicked, and based on the $index, I want the corresponding iframe to be displayed.

Answer №1

To avoid assigning the value directly to the indx variable, it is recommended to define a property within the $scope object as shown below:

   $scope.PlayFunc = function (order) {
       $scope.index = order;
   };

By creating properties and methods within the $scope object, we can easily access them in our HTML code.

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

Guidelines on populating a Vue array with data fetched from an Axios request

The v-breadcrumbs component is used to display data from the breadcrumbs array, which works seamlessly with static data. <v-row> <!-- Breadcrumbs --> <v-col class="d-flex"> <v-breadcrumbs :items="breadcrumbs"></v ...

Exploring composite search conditions in MongoDB with JavaScript

Being a newcomer to Node JS and mongo DB, I am currently working on a project that involves fetching data from mongo db using a JavaScript file. The challenge I am facing is to search multiple columns in order to retrieve the desired results. Initially, w ...

Set iframe scroll to match the height of the user's screen resolution

Can the height of an iframe scroll be adjusted to match the size of the screen (browser)? Essentially replacing the default browser scroll with the iframe scroll. Here's what I'm looking to achieve: Currently, my browser scroll is disabled: < ...

How can we effectively implement conditional rendering when dealing with components that are nearly identical?

Depending on whether the user is a professor, student, or not logged in, I render different landing pages. The landing pages are quite similar, with the only distinction being the buttons displayed. While I could easily achieve this using inline conditions ...

Preserve HTML element states upon refreshing the page

On my webpage, I have draggable and resizable DIVs that I want to save the state of so they remain the same after a page refresh. This functionality is seen in features like Facebook chat where open windows remain open even after refreshing the page. Can ...

Contrast the Django variable against the Angular variable

I am encountering a challenge while working with Django backend and Angular frontend. The issue at hand is comparing a variable value from a Django view with another variable value from Angular. I have attempted the following simple approach, but it does ...

The issue of undefined return for multiple columns in MVC when using MCAutocomplete Jquery UI

My MVC multiple column is returning undefined. What am I missing or what is wrong with my code? Please help. https://i.sstatic.net/euwe8.png Controller public ActionResult EmployeeIDSearch(string term) { // Get Tags from data ...

Keep JS Accordion open when links are clicked

Utilizing PHP, I am fetching items from a database and creating an HTML table to display each item. Adjacent to the HTML table, there is a side menu with an accordion function implemented in JavaScript. Within each accordion section, there are links for ...

Trouble with Mongoose: Data Not Being Saved

I'm encountering difficulties with a basic query on my database. While following this tutorial at https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4, the author receives a JSON object containing the name field (the only custom fi ...

What's the best way in Angular 6 to set focus on an element that's being made content editable?

I am currently utilizing the contentEditable attribute in Angular 6 to allow for editing the content of elements within an ngFor loop. Is there a way to focus on a tag element when its contentEditable attribute is set to true? <div class="tag" *ngFor= ...

`The header navigation is not responding to window resizing functionality`

I am currently developing a header navigation that consists of a logo on the left side, a profile icon on the right side, and some navigation links in the middle. A left slide menu has been implemented to trigger when the window width is less than 700px. ...

Error encountered: WebResource.axd is not found on the .net webforms website when accessed through Cloudfront

I am facing a challenge with migrating an existing .NET webforms site to work behind Cloudfront as all the webforms are breaking. Upon further investigation, it has been discovered that the site appears fine, but the webforms are breaking because the < ...

Argument not accepted in JavaScript

Currently, I am encountering a unique error message while working with a complex function that iterates through elements in my JavaScript onSuccess event. The error I am seeing is: exception encountered : {"message": "Invalid argument.", "description": " ...

Converting user input from a string to an object in JavaScript: a comprehensive guide

Is there a way to transform user input string into objects when given an array of strings? ["code:213123", "code:213123", "code:213123"] I am looking to convert this array into an array of objects with the following format: [{code: "213123"},...] ...

Using Angular to store checkbox values in an array

I'm currently developing a feature that involves generating checkboxes for each input based on the number of passengers. My goal is to capture and associate the value of each checkbox with the corresponding input. Ultimately, I aim to store these valu ...

Generating various arrays of data

I am currently facing an issue with creating separate datasets based on the month value. Despite my efforts, all month values are being combined into a single dataset in my code. Any assistance in dynamically generating different datasets would be greatly ...

Display a span element using jQuery datatable to indicate that the update operation was

I have implemented inline editing using jQuery Datatables. Currently, I am trying to display a green checkmark when a record gets updated. Below is the ajax call that populates the table: $.ajax({ url: 'api/massEditorSummary.php', type: &ap ...

Which NPM packages are necessary for implementing modular Vue components?

While I have experience with traditional multi-page applications created using HTML + JS libraries and server-side rendering (SSR), I am relatively new to modern web development. Currently, I am learning Vue JS (the latest version) through online tutorials ...

Personalized service implemented in Angular's .config settings

I've come across a few examples of how to insert custom providers into angular's .config, but I'm struggling to do it correctly. Here's the provider I have: (function() { var app = angular.module('application.providers', [& ...

Unexpected behavior found in Node.js callback responses

I am facing an issue with user authentication based on form values in AngularJS. Even after using res.send(), the controller function fails to redirect the user correctly when the username and password are correct. I'm unsure if I am handling the call ...