Using an if statement condition within an ng-repeat in AngularJS

I am currently utilizing the ng-repeat feature in AngularJS

<ul>
    <li ng-repeat="question in questions" class="question list-group-item media">
        <span class="question__type">{{ question.question }}</span>
        <br>
        <div class="btn-group question__answer justified nav-tabs">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </div>
    </li>
</ul>

My goal is to transform the following section into an if statement whereby if {{question.type}} == 'radio', then justify it on the btn-group div.

        <div class="btn-group question__answer justified nav-tabs">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </div>

Answer №1

If you want to change the text color based on a condition, you can use the ng-class directive. I have created a jsBin example to demonstrate how it works. In this example, the "justified" class changes the text color to red when the question type is 'radio'.

<div class="btn-group question__answer nav-tabs" ng-class="{justified: question.type == 'radio'}">
    <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
</div>

Answer №2

If you want to dynamically apply classes in Angular, you can make use of the ng-class directive like this:

<div class="btn-group answer__question justified nav-tabs"
   ng-class="{question.type === 'radio' ? 'radio-selected' : 'other-selected'}">
            <a class="btn {{question.type}}" ng-repeat="answer in question.answers">{{ answer }}</a>
        </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

Creating objects with variable-dependent values in node.js

I'm currently working on creating a user database in an object to assign values to each user, but I'm struggling to find a way to accomplish this. I attempted using var data = {} and then eval(`data.user_${user} = value`), however, it only write ...

The entered value in the <input> field is not valid

I am encountering an issue where the input value is auto-filled, but when I click the submit button, the input field value is not recognized unless I modify something in it, such as deleting and adding the last word to the first name. Is there a way to m ...

`Apply event bindings for onchange and other actions to multiple elements loaded via ajax within a specific div`

My form includes various input fields, dropdowns, and text areas. Upon loading, jQuery locates each element, sets its data attribute to a default value, attaches an onchange event, and triggers the change event. The issue arises when some dropdowns are d ...

Trigger a modal from one sibling Angular component to another

My application utilizes an Angular6 component architecture with the following components: <app-navbar></app-navbar> <app-dashboard></app-dashboard> The Dashboard component consists of: <app-meseros> </app-meseros> < ...

Issue with Vue plugin syntax causing component not to load

I'm facing an issue with a Vue plugin that I have. The code for the plugin is as follows: import _Vue from "vue"; import particles from "./Particles.vue"; const VueParticles = (Vue: typeof _Vue, options: unknown) => { _Vue. ...

Implement a logging system to track and record data from both incoming requests and outgoing responses on a server powered by Express and Node.js

Is there a way for my server to log the response and request data when posting to another server? Thank you. const request = require('request'); postToIotPlatform = function postToIotPlatform(req, res, next) { var formData = JSON.stringify( ...

Retrieve the HTML code stored as a string and showcase it within an Angular framework

I need assistance with extracting content from an email stored in a javascript/typescript string (message.body). The string contains HTML code like this: <div dir="ltr">test mail</div> Using angular bootstrap, I attempted the follow ...

Using React to trigger a child component's function upon clicking a tab

I need assistance with creating a React app that includes two tabs (Tab1, Tab2). When Tab2 is clicked, I want a message to appear in the console saying 'Function A is called'. Unfortunately, the code I have currently does not display the message ...

The swap feature in drag-and-drop does not have a defined function

I am currently developing a to-do app that utilizes drag and drop functionality to reorder items in the list. However, I have encountered an issue where swapping elements works perfectly for the first five elements but throws errors when dealing with the s ...

Display various JavaScript function outputs in an HTML table for convenient tracking

Thanks to a helpful user on this platform, I was able to capture data from a JavaScript function and display it in an html table. However, I now have another query. How can I execute the function multiple times during page load and record the results in ...

Setting a specific time for a div element with opacity: A step-by-step guide

Is there a way to adjust the timing for the appearance of the add-to-cart when hovering over the product-list-item? Currently, it appears when I hover over the item and disappears when I move my mouse away. .product-list-item { position: relative; w ...

Keep sending ajax request until the page is closed

Is it possible to resend an ajax request continuously using pure javascript without any framework? Here is an example of how I am currently attempting to do it: xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","demo_post2.asp",true); xmlhttp.setRequest ...

"What is the best way to have a data toggle in Vue that waits for the success or failure of

How can I ensure that the modal is triggered only after a successful axios response, without altering any existing styles? The current code triggers the modal before waiting for the axios response, despite adding v-if. Current code: <template> <d ...

AngularJS - scope watch not being triggered even after ng-class update has been made

Seeking to create a dropdown mega-menu with Angular, where clicking on Link 1 reveals its content. Here are my directives: mobile-menu manages links and menu item states menu-link is the clickable link to open/close menu items menu-item displays or hides ...

Guess the Number Game with while Loop

I have a project where I need to limit guesses to 10, display the guessed number, and keep those results on the screen after each game without replacing the previous guesses. Each set of guesses should be numbered to show how many guesses have been made us ...

arrange according to a specific sequence

I have been working on developing a JavaScript card game. I am looking for a way to match four consecutive numbers in a list. Currently, I have a loop structure that seems complex: cards = [{card:'h7'},{card:'c8'},{card:'h ...

Uh-oh! Major issue: CALL_AND_RETRY_LAST Allocation was unsuccessful - system is low on memory

I'm experiencing an issue when trying to download a CSV file. It works fine for 40,000 records but runs into a "process out of memory" error when attempting to download 80,000 records via the API. Can someone please assist with this problem? app.ge ...

Mastering the use of expressions in ng-click and ng-show in AngularJS

I've been working on creating expandable rows within a table, but I'm encountering some issues. Despite not receiving any error messages, the functionality isn't behaving as expected. I suspect there might be an issue with how I'm using ...

Generating a linked pledge with AngularJS

I need to create a linked commitment for my service provider: this.$get = function($q, $window, $rootScope) { var $facebook=$q.defer(); $rootScope.$on("fb.load", function(e, FB) { $facebook.resolve(FB); }); $facebook.api = functi ...

having trouble accessing a JavaScript array in AngularJS

Hey there, I'm currently working on a web application using AngularJS and I'm facing an issue with querying arrays. Check out the code snippet below: var angulararray = []; bindleaselistings.bindleaselistingsmethod().then(function(response) { ...