ng-repeat not recognizing directive

I recently encountered an issue with my directive that displays a larger image as a tooltip when hovering over a small image. Strangely, the directive stopped functioning properly after I placed it within an ng-repeat block. Although the directive is executing and creating the necessary tooltip elements at the bottom of the body, nothing appears upon mouseover.

Here is the HTML snippet:

<div ng-repeat="photo in photos track by $index" id="photo{{$index}}" data-tooltip-img="./photos/{{photo.large}}" big-photo>
  <a href="#">
    <img src="./photos/{{photo.small}}" alt="{{photo.name}}">
  </a>
</div>

And this is the directive implementation:

.directive('bigPhoto', function () {
    return {
        restrict: 'A',
        link: function (scope, el, attr) {
            var targetTag = "#"+attr['id'],
                xPos = (attr['tooltipPos'] && attr['tooltipPos'] === 'left') ? -304 : 20;
            angular.element(targetTag).wTooltip({
                delay: 0,
                offsetX: xPos,
                offsetY: 20,
                content: '<img src="'+attr['tooltipImg']+'" alt="'+attr['title']+'">',
                style: false
            });
        }
    };
})

For reference, here is the link to the jsFiddle demonstrating the issue: http://jsfiddle.net/codephobia/k6Ljzg7j/

Answer №1

The issue arises when attempting to access the element using angular.element(targetTag). During the execution of your directive within the ng-repeat, Angular is unable to access the element in this manner since it has not yet finished creating the element. However, the link function provides you with the element being created, so it is advisable to utilize the element provided by the link function.

Therefore, your directive should be structured as follows:


app.directive('bigPhoto', function () {
    return {
        restrict: 'A',
        link: function (scope, el, attr) {
            var targetTag = "#"+attr['id'],
                xPos = (attr['tooltipPos'] && attr['tooltipPos'] === 'left') ? -304 : 20;
            el.wTooltip({
                delay: 0,
                offsetX: xPos,
                offsetY: 20,
                content: '<img src="'+attr['tooltipImg']+'" alt="">',
                style: false
            });
        }
    };
})

See Example in Action

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

Remove leading spaces before text

Is there a way to remove spaces before text only? " with spaces between" What I want: "some text with spaces between" I have tried using text.replace(/\s/g, '') but it doesn't give the desired result: "sometextwithspacesbe ...

Leveraging the source of an image from asset variables

Lately, I've been experiencing issues with displaying images on my page, specifically when trying to show a list of images. The problem arises when attempting to store the image URL in a variable or object instead of hardcoding it directly into the s ...

Building a timer using Vue.js and moment.js

I'm currently working on a timer using vue.js and moment.js which specifically focuses on minutes and seconds. My code should technically work as intended, however it's not yielding the desired results: var app = new Vue({ el: '#app&apo ...

Securing your Node API with Passport-JWT for maximum authentication

I have been attempting to establish JWT authentication using passport-jwt. Despite following the necessary steps, I am unable to successfully execute a test GET request, and I am uncertain about how to troubleshoot the issue. Here is a summary of what I h ...

The function 'storeController' is undefined and cannot be used in this context

Looking for some guidance with my JavaScript file. Having trouble creating a new controller and receiving the error message: "Argument 'storeController' is not a function, got undefined" Any assistance would be greatly appreciated. var myApp ...

svg-import.js encountered an import error stating "Unexpected type sodipodi:namedview in SVG Import."

Currently, I am utilizing svgjs to load SVG files and perform various manipulations. In addition, I have integrated the svg-import plugin to bring in pre-existing SVG files. However, when attempting to load an SVG file, I encountered the following error: ...

Why does Axios keep timing out despite successful testing in Postman?

Trying to set up a single request for my app using axios with express/node js. Here is the code snippet that was generated through the Postman app. I have attempted different variations by creating my own form, but I always end up with the same result. co ...

Add the text received from the Ajax request to an array specifically designed for AmCharts

Hello, I'm new to JavaScript and seeking assistance. My goal is to create a chart with real-time data from my MCU, but I'm unsure about pushing a string into an array. Currently, the Array (chart.dataProvider) in this code remains undefined. var ...

Leveraging AJAX Post Data in NodeJS using Express

I'm currently working on retrieving the values I send for an ajax post within my node application. After referring to a helpful post on Stack Overflow, here is what I have implemented so far: Within Node.js: var express = require('express&apos ...

Eliminate the standard blue border that appears when control-clicking on table elements

I've encountered this question before, but unfortunately none of the solutions provided have worked for me. Some things I've attempted are: Using event.preventDefault() - did not produce the desired result. Removing user-select from CS ...

Enhancing Grails dynamic dropdown to include a pre-selected value in edit.gsp

One feature I have implemented is a dynamic dropdown menu in my _form.gsp file that appears in both the create and edit pages. While it currently works as intended, I am seeking a way to display the selected value on the edit page. _form.gsp <g:s ...

The contrastText property of the MUI React Theme palette is not functioning properly

I am working with MUI React to design a menu and I have utilized the AppBar component. I would like to customize it in the following way: brown.myBrown = '#544846'; const brownTheme = createTheme({ palette: { primary: { ma ...

Automatically closing the AppDateTimePicker modal in Vuexy theme after selecting a date

I am currently developing a Vue.js application using the Vuexy theme. One issue I have encountered is with a datetimepicker within a modal. The problem arises when I try to select a date on the datetimepicker component - it closes the modal instead of stay ...

What should you do when you need to send a message from a website to an MQTT broker that lacks Websockets support?

Currently venturing into the realm of web development, I find myself tackling a project that involves sending messages from my website to Thingstream, an MQTT broker. My initial attempt using the MQTT Paho JavaScript library was thwarted by the fact that ...

Can one recover a Javascript file from a server?

In Python, I am able to extract Javascript code from an HTML file using the code snippet below. import urllib2, jsbeautifier from bs4 import BeautifulSoup f = urllib2.urlopen("http://www.google.com.ph/") soup = BeautifulSoup(f, "lxml") script_raw = str( ...

Getting the length of a field in Internet Explorer without including the placeholder text

Is there a way to distinguish between the length of a placeholder and the length of content in a field using JavaScript when working with IE? I am facing an issue where Internet Explorer counts placeholder text as content, affecting the length calculatio ...

Clicking on the modal button causes the code to run multiple times in JQuery and JavaScript

Hello, I'm experiencing an issue where the code inside a modal is being executed multiple times when the modal button is clicked. For example, if I click the modal button once, the code runs once; if I click it twice, the code runs twice, and so on. ...

Ensuring a dependable detection of WebSocket connection status

I've been researching how to create a dependable method for recovering a WebSocket connection. After exploring various options, I discovered that one approach involves sending heartbeats (ping/pong) to the server and monitoring if the entire pong is ...

What is the most efficient way to minimize the use of if statements in an Angular component when calling a specific function?

Currently, I am working on an Angular 7 temperature conversion application. Within my formGroup, there are inputs and outputs along with two multi-select dropdowns where users can choose the unit of temperature 'From' and 'To' for conve ...

Applying a CSS rule to an external iframe using just one line of code

I need to display an external hosted iframe on a client's website and adjust the background color of the body element. Here is what I have tried: <script type="text/javascript"> function changeBackgroundColor(){ top.frames['bookings&ap ...