Is there a way to include an attribute in a class prior to a response and then eliminate it afterward?

Hey everyone, I have a situation in my HTML where I need to add a specific attribute to a class before receiving a response from an API, and then remove it after the response.

Let me show you the HTML snippet:

<div class="artist-job-search-foo artist-filter" ng-click="$ctrl.changeTypeOfFilter({params: $index + 1, paramsTwo : 'true'});$ctrl.selectedTab = $index" role="button">Audition</div>

And here is how my API call looks like:

 $scope.callFunction = function(){
    // Add attribute to class artist-filter
    // Make API call
    // Remove attribute from class artist-filter
}

Answer №1

If you're looking to explore a vast collection of resources, the Angular library available on the angularjs website is where you should head.

To address your specific scenario, utilizing ng-class will allow you to dynamically add or remove classes.

Check out this example:

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

app.controller('ctrl', function($scope, $http) {
  $scope.myClass = "artist-filter";

  $http.get("https://jsonplaceholder.typicode.com/users").then(function(response) {
    $scope.items = response.data
  });

  $scope.callFunction = function(item) {
    item.active = true;
  }

});
.deactive {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">


  <ul>
    <li ng-repeat="item in items" ng-class="{'deactive': !item.active}" ng-click="callFunction(item)">
      {{item.name}}
    </li>
  </ul>

</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

`AngularJS streamlining techniques within Factories/Services`

Recently, I delved into the world of AngularJS in an attempt to sharpen my skills. Initially, things seemed to be going well as I managed to make things work as intended. However, upon revisiting my code for a refactor, here is what I started with: window ...

The two <p> elements are being pushed to the right column, which is not their intended display

A snippet of less.css code is available at this link: #content { overflow: hidden; width: 100%; p { float: left; width: 465px; padding: 0px 25px 0px 35px; margin: 0px; br { line-height: 2. ...

Updating the bootstrap grid for printing on smaller screens by changing from col-xs to col-sm

Is there a simple way to make my webpage follow the col-sm-* grid instead of the col-xs-* grid when printed? Can the width of the print preview page be adjusted to accommodate styles for col-sm-* rules? Here's an example: window.print(); <li ...

Error parsing data in the $.ajaxSetup() function of JQuery

Currently, I am coding a program using jQuery. It was functioning perfectly in Firefox 3.5 until I upgraded to Firefox 4.0. Since then, the dreaded 'parsererror' keeps popping up and causing me quite a headache. I've pinpointed the exact pa ...

The headers in your request, specifically the token field, are not permitted by the Access-Control-Allow-Headers directive in the preflight

When attempting to make a call from my Ionic app using $http GET to the Wordpress REST API, I encountered an error: The XMLHttpRequest cannot load http://mywordpressdomain.com/wp-json/wp/v2/categories. The request header field token is not allowed by Acce ...

A space has been included before the CSS tag in the document for docx4j/Js

I am looking for a way to convert an HTML formatted string into a docx file. Currently, I am using Jsoup to clean up the HTML and then docx4j to parse the XHTML into a docx format. However, I encountered an issue with colors since they are not supported b ...

Tips for loading and updating data simultaneously in VUEJS from a single input

Currently, the data is displayed in a span tag with an input for updating it Is it possible to fetch data from an API, load it into an input field, update the input with new information, and send it back? What are the best approaches for achieving this? ...

The unusual behavior of a page refresh in jQuery when replacing content

My website features a flash player. This code references the flash player stored in player_codes.php: <script language="javascript" type="text/javascript" src="songs_related/flashmp3player/swfobject.js" ></script> <!-- Div that contains pl ...

Using CSS to overlay a fixed element with a specific z-index

HTML: <div class="wrap"> <div class="fixed"></div> </div> <div class="cover"></div> CSS: .cover{z-index: 10;} .wrap{position: relative; z-index: 1;} .fixed{position: fixed; display: block; z-index: 1;} Example: ...

After repeated attempts to initialize and destroy, Froala encounters issues when loading a textarea via Ajax

Whenever an EDIT button is clicked to update some Blog Data, it triggers an Ajax call that brings up a textarea for Froala and initiates the initialization process. This sequence works smoothly initially, but after a few cycles of edit/submit (1, 2, or 3 o ...

For each iteration, the ng-repeat will select the first value from the list

Can anyone assist with ng-repeat? I have implemented a list of values using ng-repeat. How can I specify that the middle value of the list should be shown first (specifically, I want 'Earth' to appear first in the list)? Here is my code: HTML & ...

Is the CSS hover feature not functioning properly because of the pseudo-elements?

After experimenting with SVG and CSS effects, I'm trying to achieve a similar style to the game "limbo". The grain and tiltshift effects are working as intended, but I'm having trouble triggering the hover behavior on SVG elements. What could be ...

HTML/CSS: What could be causing the iframe to not scroll properly?

I am facing an issue with my website that has 3 iframes, none of them seem to be working properly. In Firefox, the scroll bars appear but do not function, while in Chrome, the scroll bars do not appear at all. Surprisingly, they work fine in IE 6. This pr ...

Guide to obtaining scope within a nested directive

My problem lies in a nested directive within my code. I am attempting to pass a function from the nested directive to my controller, but for some reason, the function is not being triggered. Here is an excerpt of my main directive HTML: <div class="pa ...

Exploring the Art of Structuring Json Data Using jquery

After importing data from a csv file, I am trying to utilize it in a dependent dropdown feature using jquery. However, I am unsure if it is feasible to nest the received data with my current code implementation. CSV File Banco Tarjeta Cuotas Medio_Pag ...

Utilize a Javascript function to retrieve a string and set it as the ID

I'm trying to modify a div ID by appending either a 'W' or 'H' depending on whether the screen width is greater than the height. I figured I could achieve this using JavaScript since PHP lacks the ability to detect screen sizes. A ...

Display jade template with parameters that are bound using angular during rendering

Whenever I receive a request, I use a jade template and pass specific parameters to it. res.render("result.jade",{order_id: "abc123"}); I am looking for the most effective method to bind these passed parameters to scope variables when they are rendered o ...

What is the best way to reference a div link from a different PHP file?

Struggling to access a page div from another php file, I've tried calling it using (found online) admin.php#changepass Here's an example of my HTML code: <div id="changepass" class="w3-container city" style="display:none"> <form name ...

Getting the value of dynamically generated buttons when they are clicked in PHP - a simple guide

In my PHP code, I have successfully created dynamic buttons. However, I am facing an issue where I need to retrieve the value of a specific button when it is clicked, and populate all the information in a form. Below is the code snippet for handling the b ...

The HTML form label offers a choice between two alternatives

My usual method of styling forms involves organizing elements like this: <label for="CompanyNameTextBox"> <span>Company</span> <input name="CompanyNameTextBox" type="text" id="CompanyNameTextBox" /> </label> This set ...