What's the secret to AngularJS's isolate scope?

I'm struggling to add animation effects to specific buttons and I can't seem to get the isolate scope to work properly. Check out this fiddle for reference:

Fiddle https://jsfiddle.net/gLhveeor/4/

The issue is that the mouseenter event is triggering the animation for all ng-repeat items instead of just the specific one.

Your assistance with this matter would be greatly appreciated.

Answer №1

It's not about a scope problem, the issue lies in initializing the TimelineLite object with an HTMLCollection of elements and then animating all of them. It would be better to select the necessary element on mouseover as shown below:

.controller('myCtrl', function ($timeout, $scope) {
    $timeout(function () {

        var tl = new TimelineLite();
        tl.stop();

        $scope.play = function ($event) {
            var target = $event.target.querySelector('.foo-2');
            tl.to(target, 0.4, {x: 30});
            tl.play();
        };
    }, 0);
});

In your HTML code, make sure to pass the event object into the handler like this:

<div my-directive class="foo" ng-mouseenter="play($event)">

Check out the demo here: https://jsfiddle.net/gLhveeor/5/

One advice I can offer is to consider moving this logic into a directive instead of keeping it in the controller.

Answer №2

When utilizing ng-repeat, ensure that you are not assigning the same class (foo-2) to multiple elements. This can result in unexpected behavior when trying to select elements using

angular.element(document.getElementsByClassName('foo-2'));
, as it will match all elements with that class.

If the TimelineLite object is capable of initializing itself on multiple elements simultaneously, be cautious when referencing those elements. For example, in the code snippet:

tl.to($a1, 0.4, {x: 30});

The variable $a1 may contain both matched elements, resulting in t1 representing a collection of both elements. Therefore, calling t1.play() will impact both elements at once.

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

The challenge of rendering in Three.js

After configuring my code to render a geometry, I discovered that the geometry only appears on the screen when I include the following lines of code related to control: controls = new THREE.OrbitControls(camera, renderer.domElement); controls.addEventList ...

Reduce the use of if statements

Is there a way to optimize this function by reducing the number of if statements? The currentFeatures are determined by a slider in another file. The cost is updated if the currentFeatures do not match the previousFeatures, and if the user changes it back ...

Adding rows dynamically to multiple tables on a single page using a single function

I am facing a situation where I have multiple tables and a function that adds rows to these tables: <table id="table1" style=" border-collapse: collapse;"> <tr id="table1row1">... <tr id="table1row2">... <table id="table2" style=" bor ...

Using Struts2 to fetch data through AJAX and dynamically update a div element

Looking for advice on how to trigger an action class using JavaScript when a value is selected from a drop-down menu. The goal is to fetch data from a database and then update a table within a specific div without refreshing the entire page. The drop-down ...

The step-by-step guide on passing arguments and fetching results from Angular UI Bootstrap Modal through Components

I am facing a simple scenario where I have defined a modal as a component and opened that modal using `uiModal.open`. However, when I try to pass custom data to the modal using "resolve" in the open method and arguments in the controller constructor, the d ...

Is there a way to terminate an ongoing axios request?

I have been encountering a memory leak issue whenever my browser is redirected away from this component. To resolve this, I tried to cancel it using the cancel token, but unfortunately, it doesn't seem to be working as expected. I am puzzled as to why ...

What could be causing my bootstrap 3.7 carousel to not function properly?

I've been trying to implement this feature of bootstrap by following a guide, but I can't seem to figure out why it's not working. I have all the CDNs and Js Script linked in my header, directly copied from the bootstrap website. <div id ...

Display a pdf stream within a fresh window

I have a PDF document generated on the server that I need to display on the client side. The code on the server looks like this: ByteArrayOutputStream baos = generatePDF(); response.setContentType("application/pdf"); response.setHeader("Content-Dispositio ...

How to retrieve entire HTML page using an Ajax call in an ASP.NET web forms application

I'm currently working on an ASP.NET web forms application and running into an issue with making an AJAX call to a web method in the code-behind. Instead of getting the result I expect, it's returning the entire HTML page. The call is triggered b ...

How can I implement a promise loop in Angular that continues until it encounters a rejection?

My goal is to efficiently load around 10000 resources, but loading them all at once during the resolve phase is taking too long due to some calculations. I then had the idea to load the resources page by page sequentially, however, since all resources need ...

Updating the query parameters/URL in Node.js's request module

In my Express.js application, I am utilizing the npm request module to interact with an internal API. The options passed to the request function are as follows: requestOptions = { url : http://whatever.com/locations/ method : "GET", json : {}, qs : { ...

Updating line connections between objects in Three.js while rendering

I am working with a three.js canvas that contains circles moving independently. Initially, these circles are connected by a single line that follows the same geometry as their positions. However, I am facing difficulty updating the line's geometry wh ...

Having issues with ajax posting functionality

Is the following form correct? <form> <input type="submit" value="X" id="deleteButton" onclick="ConfirmChoice(<?php echo $id; ?>)" /> </form> Here is the function associated with the form: <script type='text/javasc ...

Tips for displaying data by using the append() function when the page is scrolled to the bottom

How can I use the append() function to display data when scrolling to the bottom of the page? Initially, when you load the page index.php, it will display 88888 and more br tags When you scroll to the bottom of the page, I want to show 88888 and more br ...

Utilize only certain JSON properties within JavaScript

I have access to an array of JSON objects. [ { Id: "1", StartNo: "1", ChipNo: "0", CategoryId: "0", Wave: "0", Club: "", FirstName: "Lotta", LastName: "Svenström", FullName: "Lotta Svenström", ZipCode: "24231" }, {...} ] My goal is to create a new data ...

Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side. My initial attempt involved using flex layout, but I have ...

Mongoose in Nodejs does not support the use of Aggregate as a function

Seeking assistance to rectify this issue, here is my mongoose code snippet for aggregation: export class GetVehiclesbyKotaCommandHandler { constructor(namaKota) { return new Promise((resolve, reject) => { VehiclesDB.find().populate({ ...

Undefined values in Javascript arrays

Currently, I am sending a JSON object back to a JavaScript array. The data in the array is correct (I verified this using Firebug's console.debug() feature), but when I try to access the data within the array, it shows as undefined. Below is the func ...

Pause file uploads, display modal popup, then resume uploading

Is there a way to pause file uploading in order to display file requirements before proceeding? For example, when a user clicks on "upload file", I would like to show them a modal window with details about the file they need to upload. Once they click ok, ...

You can use jQuery AJAX to submit two forms' data simultaneously in just one submission

I am looking to submit two sets of form data with just one click. <form id="form1"> <input type="text" name="name"> <input type="submit" value="Submit"> </form> <form id=&quo ...