Obtaining data from an external ng-repeat element

My list contains items that are being repeated using ng-repeat details.
I want to be able to hover over one of the li elements and have the background of a div called background (which is outside of the ng-repeat) change to the url of the corresponding details item.

However, I'm not sure how to make the external element access the data from the ng-repeat.

Check out the plunkr code snippet for more details: https://plnkr.co/edit/lGRfE9YGYN645Ztpr94K?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

    $scope.details = [
   { "name": "Employees", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" },
   { "name": "Support", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" }
    ];

});

HTML:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e4ebe2f0e9e4f7abeff6c5b4abb5abfd">[email protected]</a>" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <div class="background">
  <ul ng-repeat="detail in details">
    <li>{{detail.name}}

    </li>
  </ul>
  </div>
  </body>

</html>

Answer №1

If you want to change the background of a div based on mouseover event on an li item, you can use ng-mouseover. You can apply the background using ng-style over the div

<div ng-style="{'background-image':'url(' + bgUrl + ')'}">

DEMO

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

    $scope.details = [
   { "name": "Employees", "url":"https://i.pinimg.com/736x/76/47/9d/76479dd91dc55c2768ddccfc30a4fbf5--pikachu-halloween-costume-diy-halloween-costumes.jpg" },
   { "name": "Support", "url":"https://i.sstatic.net/IosVb.png" }
    ];
     $scope.applybackground=function(detail){
        $scope.bgUrl = detail.url;
     }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">
<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="style.css" />
</head>
<body ng-controller="MainCtrl">
  <div ng-style="{'background-image':'url(' + bgUrl + ')'}">
    <ul ng-repeat="detail in details">
      <li ng-mouseenter="applybackground(detail)">{{detail.name}}
      </li>
 </ul>
</div>
</body>

</html>

Answer №2

To access the detail object in the controller, you can utilize ng-mouseover.

<body ng-controller="MainCtrl">
  <div class="background" ng-style="{'background-image':'url({{myBackgroundUrl}})'}">
    <ul ng-repeat="detail in details">
      <li ng-mouseover="onHover(detail)">{{detail.name}}</li>
    </ul>
  </div>
</body>

Within the controller:

$scope.myBackgroundUrl = '';
$scope.onHover = function(detail) {
  console.log(detail.url)
  $scope.myBackgroundUrl = detail.url;
}

Answer №3

Implement ng-mouseover as shown below:

<div class="background" style="background-image:url('{{backGroundUrl}}')">
<ul >
    <li ng-mouseover="change(detail.url)" ng-repeat="detail in details">
      {{detail.name}}

    </li>

  $scope.change= function(url){
      $scope.backGroundUrl=url;
    }

Check out the live demo here

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

Leveraging Directives for Enhanced Leaflet Marker Functionality

Currently, I am conducting experiments involving AngularJS and Leaflet (I am still in the learning process with both). As part of this experimentation, I have noticed that I can define some HTML as a parameter in markers.bindPopup(...);. Has anyone attem ...

Problem with redirecting when using HTTPS

I recently implemented an SSL Certificate and made changes to the web.config file. Here is the updated code: <system.webServer> <rewrite> <rules> <rule name="removed by me" stopProcessing="true"> ...

When hovering over an element in Firefox, the mouse event triggers twice causing a flicker. Is there a solution to prevent this flickering issue?

CSS: <ul class="navigate"> <li> <a title="Demo01" href="###">Demo01</a> <ol> <li><a title="Demo01Children01" href="###">Demo01Children01</a></li> <li><a title ...

A powerful combination of Angular and jQuery Mobile

Is there a recommended method to combine them or should I stick with jQuery Mobile by itself? I experimented with creating a basic unordered list (ul) and the list items (li) ended up overlapping each other. ...

Tips for troubleshooting Jquery PHP echo errors

My web form code includes two fields: Name with Date and Title. Using jQuery, I have implemented a feature where the current date is displayed with a calendar, and when the date is changed in the Date field, it automatically updates in the Title field. The ...

Using JavaScript to dynamically write content to the document in the

What is the correct way to write the HTML break tag "<br>" in JavaScript without it causing a line break? I want the tag to be displayed as text. For example, "the break tag in html is ..." See below for an example of what I am looking for. <scr ...

Can a callback in an ES6 class constructor ensure that the promise is either resolved or rejected?

I'm currently puzzled by the concept of using a callback function that always returns an empty object and then passing a parameter to this callback. Visit this link for more information My assumption is that this approach ensures that the Promise wi ...

Why is it that a JSX element can take a method with parentheses or without as its child?

Why is it that when I attempt to pass a method without parentheses into a React component as a child of one of the JSX elements, an error appears in the console? However, simply adding parentheses resolves the issue. What's the deal? For example: ran ...

Exploring the nuances of receiving responses with NextJs, Nodemailer, and Fetch

Currently in the process of constructing a basic contact form with Next.js, nodemailer, and fetch. Despite successfully sending emails from the frontend form to the designated email account, the network shows the contact submission as pending. After approx ...

Redirecting with Express js when the cookie is not found

I successfully integrated Facebook login using Passport-js and also set up Cookie-strategy for traditional username/password login on my Express-js backend with a React front-end. The backend and frontend are hosted on separate servers and domains (backend ...

What could be causing issues with my Ajax and JavaScript functionality?

While developing a website to search through my python database, I encountered an issue. The non-JavaScript version was functioning flawlessly. However, when attempting to implement AJAX so that the page would not have to be refreshed each time, I faced ...

The 'Required' attribute in HTML is malfunctioning

The 'required' attribute is not functioning properly when I try to submit the form. I've searched online for a solution, but none of them have resolved my problem. Take a look at the code snippet below - I've used the required attribute ...

Can transitions be applied to links in this manner?

Having an issue with ajax requests, I am currently resorting to using JavaScript linking. This is how I normally link: window.location.href = ('file:///android_asset/www/custom/kontakty.html'); I am curious if it's possible to add a transi ...

What is the process for decoding HTML content that is wrapped within JSON data?

I have a web application built using asp.net that utilizes ajax calls. Below is the code snippet for my web method which successfully responds to the ajax call. ADController adc = new ADController(); DataTable dt = adc.GetGeneral(Convert.ToInt32(A ...

Leveraging a single jquery ajax request to handle various loops

I've been pondering if it's possible to have multiple loops, each with its own Ajax call. My goal is to create a golf scorecard where I can retrieve data for the scorecard and players in a single JSON/Ajax call... This is how I retrieve the scor ...

Adjusting the size of the div both horizontally and vertically in Angular 5 using the mouse cursor

As a beginner in Angular 5, I am looking to achieve horizontal and vertical resizing of a div by dragging with the mouse pointer. Can someone assist me in implementing this feature? ...

Whenever a query is entered, each letter creates a new individual page. What measures can be taken to avoid this?

Currently, I am working on a project that involves creating a search engine. However, I have encountered an issue where each time a user types a query, a new page is generated for every alphabet entered. For instance, typing 'fos' generates 3 pag ...

Autofill Dropdown in AngularJS Using Data from Previous Page

Despite searching extensively on StackOverFlow, I was unable to find the answer I needed. So, I am posting my question below. I have a form that includes a dropdown menu. When a user clicks a button, they are taken to a new HTML page with additional infor ...

Utilizing AngularJs for searching strings in a function

I'm currently working on implementing a search function using angularjs. However, I have encountered an issue where the function returns results based on my search query but when I delete the search query, it displays all objects in the set. Here&apos ...

Choose a specific parameter from a line using the body parser in Node.js

Upon receiving a post message, I am having trouble selecting a value from CSV data. Here is a sample of what I receive: { reader_name: '"xx-xx-xx-xx-xx-xx"', mac_address: '"name"', line_ending: '\n', field_delim: & ...