What causes the Angular expression in an ng-class within an ng-repeat to consistently evaluate to true?

I can't wrap my head around this.

Here's a scenario: I am presenting a group of objects using ng-repeat in this format:

<div ng-repeat="obj in objs">{{obj | json}}</div>

Now, I want to assign the class current to one specific <div> based on whether it corresponds to the "current object" defined by another scope variable called current.

I attempted the following approach:

<div ng-repeat="obj in objs" ng-class="{current: 'obj == current'}">{{obj | json}}, current: {{obj == current}}</div>

The interesting part is: while the expression obj == current is correctly analyzed within the div content and initially returns false for all instances when current in the scope is null, the class still gets assigned to each one, despite being contingent on the same logic.

Even if I update the value of current to reference another object, the content inside the div adjusts accordingly and displays true, but the class remains applied universally.

What am I overlooking here? Why is the class persisting even with a false evaluation?

To experiment with it: JSFiddle.

Answer №1

The issue lies in the quotes causing a failure of the code. The correct format should be:

ng-class="{'current': obj == current}"

I have made the necessary changes in your JS Fiddle: http://jsfiddle.net/qXgJm/1/

The use of quotes resulted in it always returning true, as the string itself is considered truthy.

For example: This condition would evaluate to true and output '!!!!'

if ("hello world") {
  console.log('!!!!');
}

Answer №2

Make sure to avoid using the apostrophe in your ng-class expression.

<div ng-repeat="item in items" ng-class="{active: item == active}">{{item | json}}, active: {{item == active}}</div>

Check out this example on JSFiddle

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

Issues with loading Ruby on Rails 4.0 and JavaScript in combination

I am facing an issue with my JavaScript file named "front.js" that I load from "application.js" using the usual code: //= require front Within "front.js", there are several functions that I want to trigger when the document is ready, like this: $(docume ...

The functionality of Jquery .slideToggle("slow") seems to be glitchy when used as a table expander

I attempted to implement the .slideToggle("slow"); feature to my table, following the instructions detailed here: W3Schools The toggle effect seems to be functioning, but not as expected. I want the effect to behave similar to the example on the W3School ...

Jumping Iframe Anchor Link in Src

I'm facing a challenge with an iframe positioned in the center of a webpage. I want to show content from another page within the iframe, but not starting at the very top. To achieve this, I inserted an anchor into the src of my iframe, linked to an a ...

Echarts: scatter plots linked with a line to the axis (resembling the PACF diagram)

I am currently working with echarts (js). Is there a method to link the dot of the scatter plot with the 0 value on the y-axis? I want it to resemble a pacf plot, similar to this example: The desired outcome should look something like this: https://i.sta ...

Encountering an issue when attempting to use AJAX for displaying a partial code on the current page

I'm a beginner with Rails and my current project requires me to load a specific element on the same page without affecting other elements. To achieve this, I created a new route in routes.rb as follows: get "/sell_used_car/edit", to:"se ...

"Exploring the symbiotic relationship between Node.js and Express.js: an

I recently started learning Node.js and Express.js. I used the Express.js executable (express) to create an express application, which generated the following lines in app.js: ... var app = express(); http.createServer(app).listen(app.get('port' ...

Updates to Vue.js data are not being properly reflected in the template

This is my unique text <template> <div class="unique_class"> <div class="unique_wrapper"> <div v-for="(h, index) in heights" :key="index" class="each_bar" v-bind:style="{ height: h + 'px' }"></div> ...

Tips on showcasing a collection of orders stored in a database using Vue.js

After successfully updating my orders post payment, I am wondering how I can showcase them on my Vue front end. Below is the HTML template displaying the list of orders made: <template> <div> <div v-for="order in orders&quo ...

Tips for saving and accessing Shopping Cart items using localstorage

As I develop a shopping cart for an e-commerce site, I aim to utilize browser localstorage to store the products. The functions that have been added to my code include: - addToCart(): triggered when the "Add to Cart" button is clicked. - addProduct(): ...

401 (Unauthorized) Error on retrieving data

I am currently developing a basic login feature using the HTTP Auth Interceptor Module. Within my LoginController, the code looks like this: angular.module('Authentication') .controller('LoginController', ['$scope', '$r ...

Achieving compatibility with pseudo elements :before/:after in Internet Explorer 7

I've been attempting to make these functionalities compatible with IE 7, but I'm encountering difficulties. I've downloaded and included the jQuery plugin for it in the header as shown below: <!--[if lte IE 7]> <script type="text/j ...

CORS headers present but AJAX request still fails

A request sent via AJAX from a locally hosted page to a remote server is encountering errors, despite the presence of CORS headers. The JavaScript code for this request is as follows: $.ajax({url: 'http://prox.tum.lt/420663719182/test-upload?Action=S ...

The functionality of $j is not available

I am dealing with a simple jQuery event. Within the HTML document, I have the following code: <div class="opsContainer"></div> To utilize jQuery functionality, the following script is added: <script src="https://cdnjs.cloudflare.com/ajax ...

Unavailable dates on Datepicker (information fetched from the database)

I am currently working on extracting dates from my database and storing them in an array as json data. In my MAIN.PHP file: $('#datepicker').focus(function(){ $.ajax({ url: 'getDates.p ...

Analyzing length of strings by dividing content within div tags using their unique ids

Here's my issue: I'm using AJAX to fetch a price, but the source from where it is fetched doesn't add a zero at the end of the price if it ends in zero. For example, if the price is 0.40 cents, it comes back as 0.4. Now, my objective is to t ...

What is the process for importing a specific version of a node module?

Is there a way to import a specific version of a node module? I am familiar with using npm to install packages, but that is not what I need in this case. Currently, my approach looks like this: // File 1 const fetch = require('module-version-1'); ...

javascript Href and onclick malfunctioning

I am encountering an issue with a form on my webpage: <form name="myForm" id="myForm" method="post" action="someUrl.php"> ... </form> There is a div outside of this form which contains an anchor link: <a href="anotherUrl.php" onclick="doc ...

The value of a variable remains constant even when it is assigned within a function

I developed a dynamic image slideshow using HTML and JS specifically for the Lively Wallpaper app. This app offers various customization options, which are stored in the LivelyProperties.json file along with input types such as sliders and text boxes. To ...

Center a grid of cards on the page while keeping them aligned to the left

I have a grid of cards that I need to align in the center of the page and left within the grid, adjusting responsively to different screen sizes. For example, if there are 10 cards and only 4 can fit on a row due to screen size constraints, the first two ...

Do I need to make any changes to the application when adding a new couchbase node to the cluster

Currently, I am utilizing the Node.js SDK to establish a connection with a couchbase cluster. Despite this, in the Node.js documentation, there is no clear instruction on how to input multiple IP addresses (of cluster nodes) when creating the cluster objec ...