The conditional ng-class styling will be applied to each and every div on the page

For a patient, I have a modal displaying a list of card numbers. If the card is set as the default card, it should have a grey background, based on a true value for the object.

<td ng-repeat="obj in paymentAndShipping">
            <div ng-click="setDefaultPaymentMethod(obj.ElectronicPaymentAccountType, obj.ElectronicPaymentAccountID)" ng-class="{'chosencard' : obj.PreferredAccount }">
                <span ng-show="obj.PreferredAccount" class="glyphicon glyphicon-ok"></span>
                <p>{{obj.ElectronicPaymentAccountType}} {{trimCardNum(obj.CreditCardNumber)}}</p>
                <p>Exp: {{obj.ExpirationDate}}</p>
            </div>
        </td>

Key point:

ng-class="{'chosencard' : obj.PreferredAccount }"

CSS snippet:

.chosencard {
        background-color: @gray-lighter;
    }

The code iterates over an array, retrieves objects using ng-repeat, and applies ng-class to the div inside the table cell to determine styling. It's peculiar that applying the same logic to the span inside the div works as expected, while the div itself does not - why?

A screenshot illustrating this behavior can be seen below.

Note: When clicking on an individual div, the grey background disappears from all others except the clicked one. Out of the four objects shown above, only one has a true value, which is the last card.

Answer №1

If you're wondering how to choose payment methods, take a look at the demo code snippet below:

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

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


  $scope.paymentAndShipping = [

    {
      ElectronicPaymentAccountType: "a",
      CreditCardNumber: "155652",
      ExpirationDate: "01/01/2014",
      ElectronicPaymentAccountID: 1
    }, {
      ElectronicPaymentAccountType: "b",
      CreditCardNumber: "155652",
      ExpirationDate: "01/11/2014",
      ElectronicPaymentAccountID: 2
    }, {
      ElectronicPaymentAccountType: "c",
      CreditCardNumber: "1545652",
      ExpirationDate: "21/01/2414",
      ElectronicPaymentAccountID: 3
    }, {
      ElectronicPaymentAccountType: "d",
      CreditCardNumber: "1554652",
      ExpirationDate: "31/01/2024",
      ElectronicPaymentAccountID: 4
    }

  ];

  $scope.PreferredAccount = $scope.paymentAndShipping[0];

  $scope.setDefaultPaymentMethod = function(index) {

    $scope.PreferredAccount = $scope.paymentAndShipping[index];


  }

});
.chosencard {
        background-color: yellow;
    }
td {
border:solid 1px grey
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="homeCtrl">

    <table>
      <tr>
        <td ng-repeat="obj in paymentAndShipping">
          <div ng-click="setDefaultPaymentMethod($index)" ng-class="{'chosencard' :PreferredAccount==obj }">
            <span ng-show="obj.PreferredAccount" class="glyphicon glyphicon-ok"></span>
            <p>{{obj.ElectronicPaymentAccountType}} {{obj.CreditCardNumber}}</p>
            <p>Exp: {{obj.ExpirationDate}}</p>
          </div>
        </td>
      </tr>
    </table>

<h3>PreferredAccount :</h3> {{PreferredAccount | json }}
  </div>
</div>

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

I'm looking for assistance in changing my mascot image to a pixelated image when I hover over it. Can anyone provide guidance on how to achieve this?

Seeking assistance on how to change my mascot image to a pixelated image when hovered over. Any tips or advice would be appreciated! Here is the CSS I'm currently using: .mascot { display: block; margin-left: auto; margin-right: auto; ...

Ruby Thin Server: An Unexpected ActionView Template Error

Encountering an issue when trying to deploy my project on Amazon Web Services and accessing the domain. ActionView::Template::Error (Command 'java -jar /home/ubuntu/.rvm/gems/ruby-2.1.10@silk/gems/yui-compressor-0.12.0/lib/yui/../yuicompressor-2.4.8 ...

The font color in Bootstrap is set to be lighter than the background color

Bootstrap has made great improvements in displaying navbar item colours that blend well with the background colour of the navbar. Can we apply a similar technique to body text as well? For example, if the container background is purple, can we have the t ...

Utilizing JavaScript to dynamically set a CSS file from an API in AngularJS

I am currently facing an issue with integrating a CSS file returned by an API into my entire website. Since the URL of the CSS file keeps changing, hard coding the URL is not feasible. While I have successfully implemented this on 'view1', my goa ...

What is the correct way to utilize the karma-ng-html2js-preprocessor?

I'm working on a directive called stat24hour: angular .module('app') .directive('stat24hour', stat24hour); function stat24hour(req) { var directive = { link: link, template: 'scripts/widgets/templ ...

The stature of Bootstrap's columns

Exploring Bootstrap has been an exciting journey for me, as I believe it will simplify the process of creating visually appealing web pages. I'm currently facing a challenge in understanding these two examples: Bootstrap 3: http://codepen.io/rhutchi ...

Set a consistent pixel measurement for all images

I am working on a project where I have an image of a card that needs to be repeated 30 times. Each time the card is repeated, I want it to overlap with the previous card in a specific position, resembling a deck of cards. The issue I am facing is that whe ...

Underline Rows in CSS

When designing my website, I decided to organize the content into rows using CSS instead of tables. To create a line separating each row, I used height:px; for each row manually. However, this method resulted in the line jumping around inconsistently acr ...

How can you customize the $stateProvider Provider in AngularJS?

This particular type of decorator is designed to work specifically with services and factories. However, I found it disappointing that it did not function as expected with providers. In an attempt to decorate the ui-router's $stateProvider, I tried th ...

Push the accordion tab upwards towards the top of the browser

I am working on an accordion menu that contains lengthy content. To improve user experience, I want to implement a slide effect when the accordion content is opened. Currently, when the first two menu items are opened, the content of the last item is disp ...

A possible substitute for the "background-size" CSS attribute

After struggling for days without success, I find myself once again seeking your help. The current issue revolves around the slideshow on THIS website, which is powered by the jQuery Infinite Carousel Plugin. Despite adding background-size: auto 100%; to ...

Enhance your website by adding a personalized touch with unique hover

<html> <body> <a href="test.html" style="{color: blue; background: white} :visited {color: red} :hover {background: red} :visited:hover {color: red}"> Test </a> </body> </html> What i ...

Anomaly with Responsive Images in Internet Explorer 7

Something unusual has come up. In the process of developing a WordPress theme, I needed it to be completely responsive. I decided to create a "gallery" consisting of rows with 3 thumbnails each, which would open in a lightbox when clicked. To achieve this ...

IE9 causing trouble with CSS vertical alignment

I'm trying to center a button with text using Cuffon for the font, but I can't get it to align vertically and horizontally. The text-align property is working fine, but vertical align isn't. Here's the code for my div block: btndownlo ...

the sizing issue with three div elements

I am attempting to create 3 parallel divs in HTML. The middle div should be exactly 960px wide and centered on the page, with the left and right divs positioned on either side. The minimum width of the page is set to 1024px. When the browser width exceed ...

Managing nested Angular repeats with counter

In the previous section of my document, I have a presentation layer that effectively organizes information within a shopping cart. This arrangement functions perfectly fine. At the lower portion, I employ the following code to generate form variables whic ...

Issue with Videogular: hh:mm:ss Date filter not functioning properly

I am currently working on integrating Videogular into my audio player application. Below are the settings provided in the example code on this particular page: <vg-time-display>{{ currentTime | date:'mm:ss' }}</vg-time-display> ...

Focus on the input field automatically

In my code, there are instances where I use the following: <input data-ng-disabled="SOME_SCOPE_VARIABLE" /> Additionally, I would like to have the option to use it in this way as well: <input data-ng-autofocus="SOME_SCOPE_VARIABLE" /> Alter ...

Utilizing a media query to execute a <script> exclusively on desktop devices

I am attempting to only run <script type="text/javascript" src="#" data-cfasync="false" async="async"></script> on desktop devices. I experimented with <style> @media screen and (max-width: 600px) { ...

What is the best way to wrap text and move the lines up without affecting the bottom margin?

When the text length increases in the first image, it shifts upwards similar to what you see in this image. How can I achieve this effect? The use of "word-wrap" causes the line to break and move to the next line, which is not what I want. In the code sn ...