How to switch between different ng-class styles

Within this ng-repeat loop, I intend to display the first two rows. Upon clicking "Show More," how can I switch the CSS from display:block to something else? The Show More button should toggle the visibility.

JSON

"one":[{  
       "name":"Raspberry Lemon Cake"
    }]
"two":[{  
       "name":"Raspberry Lemon Cake"
    }]
"three":[{  
       "name":"Raspberry Lemon Cake"
    }]
"four":[{  
       "name":"Raspberry Lemon Cake"
    }]
"five":[{  
       "name":"Raspberry Lemon Cake"
    }]

HTML

<div class="GridBrd" ng-repeat="Detail in Results" 
     ng-class='{showflight: $index > 1}'></div>

<span class="moreOpt" ng-click="show()">
       <a href="javascript:void(0);">Show More</a>
</span>

CSS

.showflight{
    display:none;
}

Script

$scope.show = function (){
    ***----How can I manipulate the ng-class here to change between block and none?----***
}

Answer №1

Another approach you may consider is utilizing the limitTo built-in filter within your ngRepeat. The concept involves having a button that can toggle the expression, causing the limitTo to adjust to the maximum item length when showAll is true and defaulting to 2 when false;

For example:

<button ng-click="$ctrl.showAll = !$ctrl.showAll">Toggle</button>

<ul>
  <li ng-repeat="item in $ctrl.items | limitTo: ( $ctrl.showAll ? $ctrl.items.length : 2 )">{{item}}</li>
</ul>

See demo in action

Answer №2

<div ng-class="{ 'displayBlock' : isDisplayed,  'displayNone' : !isDisplayed }">
   ...
</div>

CSS:

.displayBlock {
    display: block;
}
.displayNone {
    display: none;
}

and in controller.js:

$scope.isDisplayed = false;
if(your condition) {
   $scope.isDisplayed = true;
}

This method allows you to dynamically change CSS classes in Angular. It may seem like a lot of effort, but it can be quite useful.

Answer №3

Give this a shot:

HTML Challenge

<div class="GridBox" ng-repeat="Data in FinalResults" 
 ng-class='{highlight: $index > 5 && !showAll}'>  </div>

<span class="readMore" ng-click="showAll = !showAll">
   <a href="javascript:void(0);">Expand Section</a>
</span>

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

Passing data from JavaScript to PHP

Seeking assistance with a JavaScript and PHP issue. I have a script that sends an ID to a PHP page in order to retrieve details. <script> $(document).ready(function() { displayListings(); $("#listTb").on("click", "tr", function ...

I find the SetInterval loop to be quite perplexing

HTML <div id="backspace" ng-click="deleteString(''); decrementCursor();"> JS <script> $scope.deleteString = function() { if($scope.cursorPosVal > 0){ //$scope.name = $scope.name - letter; ...

Create a custom chrome browser extension designed specifically for sharing posts on

I'm working on creating a basic chrome extension that features an icon. When the icon is clicked, I want the official Twitter window to pop up (similar to what you see here). One common issue with existing extensions is that the Twitter window remains ...

Is there a way to direct the user's cursor to a specific location on the page when they hover over an image?

I'm interested in creating a script that can manipulate the user's mouse position when they hover over an image. For example, if I hover over the image, I want the mouse to be dragged lower towards a specific text. Is there a method to achieve th ...

Leveraging Ajax for establishing session and displaying outputs

Trying my best to make this work with AJAX, but it's a new concept for me. So bear with me as I figure this out... I've posted a couple of questions about resolving the issue at hand and we've made progress. However, a new challenge has pre ...

Using Jquery Chosen Plugin to Dynamically Populate One Chosen Selection Based on Another

Good evening to all, please excuse any errors in my English. I have successfully integrated a jQuery Chosen plugin with my 'estado' field (or province). My goal is to populate another jQuery Chosen plugin with the cities corresponding to that s ...

Exploring the Bookmarking Capabilities of Firefox

Is there a way to access the users' bookmarks using Firefox API and javascript? Appreciate any help, Bruno ...

V5 Modal & jQuery: troubleshooting the spinner problem during loading of content

I'm working on displaying a spinner while loading modal content with the use of bootstrap v5 modal and jQuery. However, I encountered some issues in my example. The spinner does not display again after closing the modal; it only shows for the first t ...

floating downward inside the container

Check out my website at . Everything looks great in Firefox, but when I view it in IE, the "top" button that should be floated inside the h2 titled "Unit 301: Pre-production skills" is floating to the right and getting pushed outside of the heading. Any i ...

Tips on condensing lengthy text into a single line using Bootstrap 4

I have been searching for a way to truncate long text into a single line in bootstrap 4. I have come across several solutions on Stack Overflow such as Link 1, Link 2, Link 3, but none of them seem to work for me. Here is the text I am dealing with: http ...

Conceal the scroll bar while still allowing for scrolling functionality

In this code snippet, I am trying to maintain the scroll position of two blocks by syncing them together. Specifically, I want to hide the scrollbar of the left block while scrolling the right one. If anyone has any suggestions or solutions for achieving ...

Trigger a notification from one webpage to another (PHP, JavaScript, HTML)

I'm currently working on a website that consists of both a receptionist page and a user page with multiple logins. The receptionist page displays a table listing all logged-in users, including their status (either ready or busy). This table is refresh ...

Refreshing a component in Angular/Angular2 using routerLink from the NavBar when already on the current route

When I am on the same route and click again from the navbar, nothing happens. Is there a way to refresh my component directly from the navbar using a method in routerLink? <li [routerLinkActive]="['active']"><a [routerLink]="['/ca ...

Is there a way to segregate method calls when there are two distinct paths that lead to the same destination?

Currently, I am in the process of creating an express app that consists of a login page, a signup page, and a display page. Both the signup and login pages redirect to the display page where only the username is displayed. However, I am interested in findi ...

Testing the state of an Angular application by using Jasmine to mock resolve

I'm currently working on simulating the resolve functions in the $state object of my ui-router file, but I am facing some challenges in getting it to function properly. Here is a snippet of my router code: $stateProvider .state('state.of.page&ap ...

Determine if the JQuery change event is not triggered by a hyperlink

I have integrated the Nestable script from this source and I'm looking for a way to include clickable links in the navigation items that can be moved around. Currently, adding a link to the <li> element causes the entire navigation to behave un ...

Retrieve numerical data from the element and enclose it within a span tag

My goal was to indent a number, but the HTML generated from my CMS is making it difficult to target the number and apply a specific style. <div class="content"> <b>01. Header</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...

a beginner's guide to utilizing the grunt target

Here is an example of my Gruntfile.js: 'use strict'; module.exports = function(grunt) { grunt.initConfig({ grunt.config.set('targ', grunt.option('target')); cachebuster: { build: { ...

Modifying the color of specific sections of SVG elements

Interested in utilizing the d3.js timeknots component, a svg visualization that includes line and circle elements. My goal is to implement a stopwatch animation that dynamically changes the color of the svg visualization over time. I am contemplating crea ...

Issue with the functionality of Bootstrap 5 dismissable alert needs to be addressed

I encountered an issue with the alert in my html code: <div class="alert alert-success alert-dismissible fade show d-flex align-items-center justify-content-center" id="alert" style="display:none;"> <button type=& ...