The ng-change directive is used on a dropdown to modify the styling of another HTML element

I need the icon to the left of the input in my HTML form to change based on whether the user selects male or female. However, I am facing an issue where when switching from nothing to female, the icon does not update immediately (even though the ng-change function is triggered and works properly). The class only updates once the switch happens again.

<span class="input-group-addon"><i class="fa fa-male fa-lg fa-fw" ng-class="genderIcon"></i></span>
<select id="gender" name="gender" class="form-control input-lg" ng-model="form.gender" ng-change="switchGender()">
    <option value="" selected="selected">Select Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

Below is the switchGender function:

$scope.switchGender = function() {
    $scope.genderIcon = ($scope.form.gender == 'Male') ? 'fa-male' : 'fa-female';
    console.log($scope.genderIcon);
}

Even though the correct class is logged in the console, it does not reflect immediately in the view. I am confused as to why this delay occurs despite having the right icon variable?

Answer №1

Angular provides a useful directive called ng-class, which allows you to apply classes directly to the UI.

When you use the ng-class directive, Angular creates a watcher internally. This watcher is triggered whenever any scope variable is updated. The expression inside the ng-class is evaluated on each digest cycle, and the resulting class is applied to the class attribute of the element.

HTML

<span class="input-group-addon"><i class="fa fa-lg fa-fw" ng-class="{form.gender == 'Male' ? 'fa-male' : 'fa-female'}"></i></span>
<select id="gender" name="gender" class="form-control input-lg" ng-model="form.gender" ng-change="switchGender()">
    <option value="" selected="selected">Select Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

Check out this Plunkr for a working example

I hope this explanation helps resolve your issue. Thank you.

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

The algorithm for Strict Equality Comparison when applied to an identical object

While reviewing the Vue.js source code, I came across a contentious if statement. This part defines a reactive setter for a property. if (newVal === value || (newVal !== newVal && value !== value)) { return } I have consulted this strict equali ...

Creating an object in AngularJS by merging data from three separate API calls

I am looking to develop a Jenkins dashboard using AngularJS. I am considering combining data from three different API calls to create an object that can be used in the HTML file with ng-repeat, but I'm not sure if this is the best approach. The desir ...

What are the proper methods for accurately testing vuex state and mutations?

Can someone guide me on how to properly test mutations and state? I have a modal window component that is rendered when the showModal property in the state is true. There is an event triggering a mutation that changes this property. How can I verify that a ...

What is the best way to dismiss the additional modal popup?

Here is an example that I need help with: http://jsfiddle.net/zidski/Mz9QU/1/ When clicking on Link2 followed by Link1, I would like the Link2 box to close automatically. Is there anyone who can assist me with this issue? ...

Drawing a line beneath the mouse's center using JavaScript

Struggling with my JavaScript drawing tool, particularly the draw() function. The line is consistently off-center below the mouse cursor. How do I fix this issue? My aim is to have the line always follow the center of the mouse as it moves. Could someone c ...

How to remove the black border on a material-ui button

I am currently working with the material-ui datepicker and buttons. Whenever I click on the datepicker icon to select a date or any of the buttons, I notice a black border appearing. How can I remove this black border from the design? https://i.sstatic.ne ...

Issue encountered when utilizing FontAwesome in xhtml file in jdeveloper

Attempting to integrate font-awesome into my .xhtml page is proving to be a challenge, resulting in errors as shown in the image below. My development environment is Jdeveloper 12.2.1. Despite researching various topics, I have yet to find a solution. An ...

Error: Unable to access the property 'map' as it is undefined | React, Redux

I'm encountering an issue that says: TypeError: Cannot read property 'map' of undefined. This error is related to trying to map over the array (posts) when it's empty: const postsList = posts.map((postList, i) => { Here is my actio ...

Having trouble with Ajax retrieving the updated JSON file version

I'm pretty new to coding and terminology in general, so I've done my best to simplify my code, although it might still have redundancies. Appreciate your patience in advance. My task involves using an ajax and php script to write data to a file ...

Performing a simulated click on a dynamically inserted element using pure JavaScript

I am faced with the challenge of programmatically navigating a ReactJS-based website in a looped workflow. The process involves clicking on Element1, dynamically altering the web page to add Element2, then clicking on Element2, and so on until eventually r ...

Organize JSON information in a tabular layout

After resolving my previous issue, I am now attempting to store JSON data in cookies and then retrieve it to organize the variables in tabular form. However, I am uncertain about how to go about arranging it. View the demonstration here ...

Ways to efficiently incorporate data into App.vue from the constructor

My app initialization uses main.js in the following way, import App from './App.vue'; const store = { items: [{ todo: 'Clean Apartment.', },{ todo: 'Mow the lawn!', },{ todo: 'Pick up ...

Issue encountered when attempting to modify the directive when the drop-down list is changed in AngularJS

Experiencing issues updating the directive when the drop down list is changed using AngularJS. Below is my application code: HTML Code <div ng-app="myApp" ng-controller="MyCtrl"> <select ng-model="opt" ng-options="font.title for font in font ...

Is there a way to extract the primary color of Windows 10 specifically for use in Electron applications?

Recently, I had the opportunity to use an Electron app called Knote which had a unique feature that allowed users to match the main color of the application with Windows 10. However, it seems that this feature has been removed. This led me to wonder if the ...

Error: An unexpected identifier was found within the public players code, causing a SyntaxError

As a newcomer to jasmine and test cases, I am endeavoring to create test cases for my JavaScript code in fiddle. However, I'm encountering an error: Uncaught SyntaxError: Unexpected identifier Could you guide me on how to rectify this issue? Below is ...

What is the procedure for setting up the typings folder in AngularJS 1.5 with TypeScript?

I'm currently working on a project using AngularJS 1.5 and TypeScript. I need to install the "angularjs/angular.d.ts" and "angularjs/angular-route.d.ts" files. Despite installing tsd globally, when I run the command "tsd install angular," I receive a ...

PhpStorm 2019.2 introduces Material UI components that have optional props instead of being mandatory

My PhpStorm 2019.2 keeps showing me a notification that the Button component from Material UI needs to have an added href prop because it is required. However, when I refer to the Material UI API, I see something different. Take a look at this screenshot: ...

Coding in PHP, JavaScript, and HTML allows builders

I am facing some difficulties in locating my specific question, so I will describe it here. Currently, I am working with an oracle database and integrating it into an HTML website using javascript and php. I have successfully displayed the php file, but th ...

I am finding the event naming conventions in Vue 3 to be quite perplex

In the parent component, there is a child component: <upsetting-moment-step :this-step-number="1" :current-step-number="currentStepNumber" @showNextStep="showNextStep" ></upsetting-moment-step> The par ...

Transmitting data as an array using JQuery

$('[data-toggle="mftapproveCheck"]').click(function () { var selected = $("#checkboxes input:checked").map(function (i, el) { return el.value; }).get(); //alert("selected = [" + selected + "]\nas int = \"" + selected.join(";") ...