Problem with traversing in jQlite .find() function not functioning as expected

I am encountering a problem while using an angular directive and attempting to utilize the jQlite .find() method:

DIRECTIVE

function cardsList () {
    return {
        restrict: 'A',
        controller: 'CardsController',
        templateUrl: 'app/directives/cards-list/cards-list.html',
        link: function ($scope, $element, attr, CardsController) {
                var cardLink = $element.find('a');

                console.log(cardLink);
            });

        }
    }
}

contextCards.directive('cardsList', cardsList);

An empty [] is being displayed on the console.

TEMPLATE

<li data-ng-repeat="card in cards" class="cards--item">
    <a class="cards--link" data-ng-href="#/{{ card.slug }}">{{ card.title }}</a>
</li>

VIEW

<ul class="col-xs-12 cards--list" cards-list></ul>

I am attempting to access the <a> elements. As per the documentation, .find() only functions with tag names, which aligns with my current goal.

EDIT: My objective is to assign a class to the <a></a> if it represents a selected card (such as .current-card)

Answer №1

It appears that the method of specifying the selected card in the model is not clearly defined in your answer. For the purpose of clarification, I am assuming that the card object (which represents each iteration of ng-repeat) includes a flag called card.isSelected.

To handle this scenario, you can utilize ng-class to determine which CSS class should be added based on the value of this flag:

<li ng-repeat="card in cards" class="cards--item">
    <a class="cards--link" 
       ng-class="{'current-card': card.isSelected}"
       ng-href="#/{{ card.slug }}">{{ card.title }}</a>
</li>

Additional Information:

In response to your original inquiry regarding why .find("a") is returning empty, it is due to the behavior of the ngRepeat directive, which transcludes its content by removing elements from the DOM during compilation and placing them at a later stage than your link function.

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

Generating a new DOM element for each AJAX response received

I am working on a project to build an application that fetches data about restaurants and places of interest from the Yelp API and then generates a materialize "card" for each response. Below is the code I have so far. function callback(results, status) ...

Utilizing JSONP callbacks in Dart

I've been struggling with implementing basic JSONP in Dart and I can't seem to figure it out. After reading this specific blog post along with another helpful resource, it suggests using window.on.message.add(dataReceived); to receive a MessageEv ...

Unusual encoding in the ajax response

I am trying to use the Ajax function from Jquery to send a string to my JavaScript file, but I am encountering an issue where weird question marks or diamond blocks are appearing before the string that I expect to receive. (function( $ ) { $.f ...

Can the jQuery Cycle Plugin: scrollHorz with Prev/Next automatically scroll without any input from the user?

Currently implementing the jQuery cycle plugin: jquery.cycle.all.min.js - newest version (2.8.8) available at http://malsup.com/jquery/cycle/ The scrollHorz effect is being used with previous and next links, functioning perfectly except for one issue: upo ...

Issue with ngTable: Error retrieving data for server-side pagination

I am currently working on setting up a server-side table using ng-table. However, I am encountering some issues with the getData function. It keeps giving me errors such as $defer.resolve is not a function or params is not defined. I noticed that I can ac ...

What is the method for positioning the cursor at the beginning of a center-aligned placeholder within a contenteditable element?

Here is the code snippet I am working with: HTML: <div class="title" contenteditable="true" placeholder="Title"></div> CSS: .title { width: 500px; margin: 0 auto; text-align: center; } [contenteditable=true]:empty:before{ conte ...

Troubleshooting a hybrid Angular Ng1/Ng2 application that is causing an error stating 'Provider for $scope not found'

I am currently in the process of developing a hybrid Ng1/Ng2 app utilizing NgUpgrade as part of our migration plan to transition towards Angular 2. Initially, the bootstrap appears to be functioning correctly: platformBrowserDynamic().bootstrapModule(App ...

AngularJs input field with a dynamic ng-model for real-time data binding

Currently facing an issue with my static template on the render page. <form name="AddArticle" ng-submit="addArticle()" class="form add-article"> <input type="text" value="first" init-from-form ng-model="article.text[0]" /> <input typ ...

Preserving the Selected Date on the Calendar Even After the PHP Variable is Passed

I am currently using the calendar code below with a slight modification that I implemented. However, I have encountered an issue where when I select a date, the calendar successfully highlights the selected date. But once I pass this selected date along ...

Using Node.js and MongoDB to filter a sub array within an array of objects

Hello everyone, I currently have an array of objects containing some populated fields. Below is the product schema: import mongoose, { Schema } from 'mongoose'; const productSchema = new mongoose.Schema( { name: String, description: S ...

Having trouble accessing the dashboard after uploading a document to Firestore

I am currently working on a project where I need to upload an audio file to Firebase storage and also add document data related to the audio file in Firestore database. The process involves recording the audio, uploading it to Firebase storage, submitting ...

Displaying temporary data and removing it from an HTML table: Tips and Tricks

There are three input boxes and a button below them. When the button is clicked, I would like the input data to appear in an HTML table where I can delete records. I would greatly appreciate any assistance. Thank you in advance. ...

Html file with capabilities similar to Datatables for exporting data as a single, compressed file

I managed to create a single HTML table with export options, search functionality, and pagination by utilizing DataTables with static data. plnkr.co/edit/n3cbx8GrGoJtOpgbxE32?p=preview Is there a similar example or working HTML code available in angular ...

Dynamic manipulation of classes based on user attributes

One thing I've noticed is that certain WordPress themes, like 'Thematic', include user-specific classes in the body element to avoid using CSS browser hacks. For example: wordpress y2010 m02 d26 h05 home singular slug-home page pageid-94 pa ...

Errors are raised when trying to watch a formatted date string in Angular

Within one of my Angular 1.x controllers, I have implemented a watch statement as follows: $scope.$watch($scope.someData.timestamp, doSomething); The timestamp attribute can be either null, undefined, or a timestamp formatted string like "2016-05-31T09: ...

FusionCharts Gauges may share similar code, but each one produces a unique output

Currently, I am developing a project that involves creating a dashboard with 3 gauges. These gauges are enclosed in bootstrap cards with a column value set to 4. In the layout of the dashboard, these 3 cards are positioned next to each other. I have succe ...

Removing a delete option in ReactJs excluding the Component

Can someone help me with this issue? I am trying to use the delete button but I keep receiving an error. Thank you in advance for your assistance. Below is the code where I am trying to access the properties of a Toy class parent and then delete by Id usi ...

Display an alert using JavaScript once the RadGrid Telerik web component has finished exporting data

I'm currently utilizing a Telerik web component called RadGrid that is connected to an Asp.Net ObjectDataSource. This component allows the data it is linked to be exported into Excel, PDF, or Word formats. However, I am facing an issue where I am unab ...

Error: Module not found - The package path "." is not exported from the specified package. As a result, Firebase will not update in your Next.js

I have been developing a Next.js application that retrieves data from a Firebase collection. During the process of connecting to the Firebase database, I have come across the following error: Failed to compile. Module not found This error seems to be ori ...

When I set up the proxy in Vite, it redirects me to the specified proxy URL on my local server. I specifically intend to utilize it solely for making API

Below is my Vite configuration file vite.config.ts: import { defineConfig } from 'vitest/config' import vue from '@vitejs/plugin-vue' import { quasar, transformAssetUrls } from '@quasar/vite-plugin' const path = require(&apos ...