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

Height of the image is being boosted while the width remains consistent

When working on a responsive page layout, I encountered an issue with increasing the height of an image while keeping the width constant. Modifying the width of the image reflects changes, but adjusting the height does not seem to make any difference. I t ...

What is the process of turning a picture from a menu file into a clickable link?

I have created a menu with some images included, and I want them to be clickable links. However, currently only the text on top of the images acts as a link when hovered over. I would like the entire image to be clickable, not just the text. I believe I ...

Encountering the error message 'Invalid cast specified' while using SqlDataReader in MVC

I kindly request not to categorize this inquiry as a DUPLICATE QUESTION. Despite my efforts to follow all the suggested solutions, I am still encountering this error. Is there anyone who can provide insight on where this error originates from? Below is th ...

Can text be formatted differently based on its length and whether it wraps onto multiple lines?

Looking to display book titles on a webpage with varying lengths. Some are short enough to fit in one line, while others are longer and wrap onto two lines. Is it possible to apply different styles (such as color or line-height) for these two cases using ...

Unable to locate the Bootstrap CSS file when deploying on PythonAnywhere

I recently created an admin panel that integrates with a database. To automate the process of generating the admin panel, I utilized flask-admin. Running the server locally on my PC displayed the bootstrap swatch correctly and everything worked seamlessly. ...

Top Margin: Supported by Chrome and Safari

After troubleshooting why the margin-top is not working on Safari, but works fine on Chrome, I discovered a discrepancy. Chrome Upon hovering over an item in the image, the cursor changes as expected. This feature operates smoothly in Chrome. https://i. ...

The elements are out of sync and not properly aligned

Having trouble creating a navbar and encountering two issues. The search text bar's width is not 100%, it should fill the width to 100% of its column. The Glyphicons are not vertically aligned. For reference, you can check out this JSFiddle link A ...

angularsjs state provider with multiple parameters

I am struggling to create a state provider that can handle multiple parameters. Is it possible to capture them as an object or array, or do I have to capture them as a string and then separate them? For example, this is my current provider: .state(' ...

Tips for creating a dropdown that autocompletes similar to the one found in Google Chrome

Looking to create a unique custom drop-down autocomplete suggestion list using HTML and CSS, similar to Chrome's design (shown below). In my attempts to achieve this, I utilized an absolutely positioned table beneath the input element. I applied vert ...

Tips on dividing the information in AngularJS

Sample JS code: $scope.data={"0.19", "C:0.13", "C:0.196|D:0.23"} .filter('formatData', function () { return function (input) { if (input.indexOf(":") != -1) { var output = input .split ...

Creating personalized dots in the owl carousel

I have successfully added custom previous and next buttons to my carousel using Owl Carousel, but I am having trouble displaying the navigation dots. Can anyone help me identify where I might have made a mistake? // owl.carousel.css .owl-controls { ...

What is the easiest way to modify the color of a basic PNG image in a web browser?

While working on a website project, I encountered instructions for mouseover styles in the design that got me thinking: It's straightforward to achieve with javascript or css image swapping... but here's the catch. There will be multiple icon li ...

Choose the option for overseeing safaris

Hello there! I need some help with Safari. Can you please guide me on how to disable the arrows? https://i.stack.imgur.com/1gzat.png ...

How to position the play button in the center using Material UI and Flexbox

Seeking advice on achieving a centered play button with borders in this layout. Struggling with flexbox to position the play button within the code provided. function Tasks(props) { const { classes } = props; return ( <div className={classe ...

CSS - Desiring a border with three layers

Here is the CSS code I am using: border: 2px solid #00ff60; outline: 1px solid #000; outline-offset: 0px; This is how it looks: https://i.sstatic.net/SuvU1.png Is there a way to modify it so that it includes an inner black border, similar to the one at ...

Adjust the button's color based on the selected color from the palette

I have a selection of vibrant colors displayed in a square button, arranged like this: <div class="dropdown color-picker-dd"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"></button> <div ...

Creating visually appealing tables with nested tables using CSS for styling including borders and backgrounds

Looking to create webpages that have a variety of tables, including a main table with a header (marked 2 on the picture) and several data tables for the user (marked 1 on the picture). To style these specific data tables, I've added some custom CSS: . ...

What is the best way to adjust the width of a column grid header in extjs

When adjusting the width of a vertical header in a grid to 50px, the header text disappears unless manually dragged to the left. How can I ensure the header text remains visible even with a smaller header size? Consider the following code snippet: { text ...

Inject a directive attribute dynamically into an element using another directive, and ensure the initial directive is set to 'active.'

Let me explain in more detail. I am utilizing the tooltip directive from the UI Bootstrap suite, which allows me to attach a tooltip to an input element like this: <input type="text" ng-model="inputModel" class="form-control" placeholder=" ...