Tips for managing multiple ng-show and one ng-hide in AngularJS: when one condition is true, set the ng-show for that condition to true and the rest to false

Whenever a user hovers over the stars, my goal is to display the message

You rated <b>{{rate1}} star.</b><a ng-click="showMe()" class="modifyIt"><b  > modify?</b></a>
. If the user has already clicked on the rating, I want to show 'Thanks for rating' (refer to the HTML below for my implementation). However, the issue arises when it always shows 'Thanks for rating' once the user has rated. How can this be resolved?

Below is my attempt at achieving this behavior. Any guidance on where I may have gone wrong would be greatly appreciated. Thank you!

<div class="user">

  <uib-rating ng-model="rate" max="5" read-only="isReadonly" on-titles="['one','two','three']" aria-labelledby="default-rating" class="readOnlyRating "></uib-rating>


  <div  class=" arrow_box rt" ng-show="showRatings">
    <div class="ratingName">
      <h5><b>Give your rating here..</b></h5>
    </div>
    <div class="stars">
      <uib-rating ng-model="rate1" ng-click="rating(rate1)" max="5" read-only="isReadonly1" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" aria-labelledby="default-rating" class="readOnlyRating "></uib-rating>
    </div>
  </div>
  <div ng-mouseleave="hoverOut()" class="arrow_box rt" ng-show="ratevalue ">
    <h5 class="ratedPopover"> Thanks for rating </h5>
  </div>

  <div class="arrow_box rt" ng-hide="showRatings">
    <h5 class="ratedPopover">You rated <b>{{rate1}} star.</b><a ng-click="showMe()" class="modifyIt"><b  > modify?</b></a>
  </h5>
  </div>

</div>

Here is the code snippet from my directive file

scope.rating = function (rate) {

    scope.ratevalue = rate;
    if ($localStorage[localStorageRatingKey] == undefined) {
        previousRatingValue = 0;
        $localStorage[localStorageRatingKey] = scope.ratevalue;
    } else {
        previousRatingValue = $localStorage[localStorageRatingKey];
        $localStorage[localStorageRatingKey] = scope.ratevalue;
    }

    ratingService.update({
        companyId : scope.details._id,
        userRating : scope.ratevalue,
        previousRatingValue : previousRatingValue
    }, scope.details, successCallback, errorCallback);

    function successCallback(res) {
        // console.log("coming from callback");
        scope.rate = res.avgRatings;
        scope.reviewsCount = res.totalRatingsCount;
    }

    function errorCallback(res) {
        NotificationFactory.error('Failed to update the product rating...', res.data.message);
    }

};

scope.showMe = function () {
    scope.showRatings = !scope.showRatings;
    console.log("showme :" + scope.showRatings);
}

scope.hoverOut = function () {
    if ($localStorage[localStorageRatingKey]) {
        scope.showRatings = !scope.showRatings;
    } else {
        scope.showRatings = true;
    }
    console.log("hoverOut ShowRatings:" + scope.showRatings);
}

if ($localStorage[localStorageRatingKey]) {
    scope.showRatings = false;
} else {
    scope.showRatings = true;
}

Answer №1

Instead of displaying the message 'Thanks for rating' using ratevalue, I recommend implementing a flag named hasjustRated. Begin by initializing this flag to false in your code.

Within the function definition:

scope.rating = function (rate) { ... }
, add this initialization at the start.

After the success callback, include the following lines:

scope.hasjustRated = true;

When exiting hover, reset the value to false. Consider creating a clear function specifically for handling actions when leaving hover.

The purpose of the showMe function is somewhat unclear.

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

Tool designed to analyze the timing of sub requests and methods in Node for benchmarking purposes

For my benchmarking and load testing needs, I initially utilized tools such as Apache Bench, Siege, and benchmark.js. However, these tools only provided me with the overall result or time taken from start to finish of the test. I am now seeking a tool or l ...

My controller is playing hide and seek with Ionic and it seems

I have been working on integrating a side menu into my app, which required me to make changes to how the controllers are set up. I introduced a menu controller, but now I keep encountering this error message: Argument 'MenuCtrl' is not a functio ...

Using AJAX to connect the JSP page with the Servlet's doGet() function for interaction

Currently, I am working on the JAVA code provided below in order to make a call to the servlet's doGet() method from a JSP page using an AJAX request. This is how my AJAX call looks like: I am passing the clicked text captured by ng-click of Ang ...

JavaScript: Extending a class with an invalid or null value is not permitted

Trying my hand at constructing a page object for login testing with WebdriverIO. Encountering the error ERROR: Class extends value #<Page> is not a function or null on line 3 of login.page.js. No clue what mistake I'm making... Is there a wron ...

What is the process of handling the event queue in Node.js?

Exploring the intricacies of the Node.js architecture has left me with several questions: 1) Is the event loop a component of libuv or v8? 2) How does the callback in the event queue transition to the call stack for execution after being delegated by the ...

Press the Radio Button to automatically submit the form in ASP.Net Core

While working with ASP.Net Core, I encountered a scenario where I needed to update the page based on the radio button that a user clicks. I wanted the page to refresh automatically to display new data depending on the selected radio button. Would it be be ...

Challenges with Knockout.js Virtual Elements in Different Environments

I am facing a peculiar issue where a virtual knockout template fails to bind correctly when accessed remotely, yet functions perfectly when viewed locally. You can find the problematic page here: Here is the template I am using: <ul> <!-- k ...

Automatic logoff will occur after 15 minutes of inactivity in C# programming language

After a period of 15 minutes, the following method logs out the user. However, it currently logs out the user even if they are active. I am seeking a solution where the method will only log out the user if they have been inactive for the full 15 minutes. ...

Automatically populate textboxes with database information based on selected combobox item without the need to refresh the page

I would like to implement a feature where textboxes are automatically filled from a database when a user selects an option from a combo box. This can be achieved using jQuery or JavaScript in PHP. Once a user selects an element, the corresponding text sh ...

Violation of Invariant: Incorrect hook usage or a cross-origin issue has occurred

After successfully getting my function to work, I decided to implement a loop for feedback from the backend post SSR. Wanting to utilize hooks, I converted it into a functional component and began writing code. However, even with an empty hook, I encounter ...

When the result of Ajax is identical to that obtained without Ajax, the innerHTML remains the same

I understand that utilizing innerhtml is generally considered a poor practice due to the potential for XSS vulnerabilities (). However, consider the scenario below: I have a webpage generated through a Twig template called index.html.twig. When using te ...

Update header component dynamically upon successful login with Angular 11

I am currently using Angular 11 and facing an issue with displaying the username in the header component. The header loads before the login component, which results in the user data being stored in local storage only after the login component is loaded. As ...

What is the best way to run a callback once several Ajax requests have finished?

I am facing a challenge with executing a callback function after multiple jQuery Ajax requests have been completed. The issue arises when these Ajax requests call another function, resulting in the functions being undefined. I suspect that the root of ...

What is the best way to invoke a service within the angularJs configuration settings?

I'm encountering an issue with a service that needs to be called in the app's config module. It's throwing an error when I attempt to inject it. Strangely enough, this service functions perfectly when injected and executed in controllers. An ...

Messy code appeared when sending an AJAX post to JBoss EAP 7 without using encodeURIComponent

Initially, the project functions smoothly on tomcat using UTF-8 and jboss eap 6 with UTF-8 page encoding as well. Additionally, the jboss configuration includes: <servlet-container name="default" default-buffer-cache="default" stack-trace-on-error="loc ...

A step-by-step guide to parsing a JSON string with jQuery

Attempting to extract data from a JSON string using jQuery, but encountering issues with retrieving values. var jsonString = '{"data":{"2G":[{"amount":"9","detail":"35 MB 2G Data , Post 35 MB you will be charged at 4p\/10kb","validity":"1 Day"," ...

I'm new to React and curious - why do we need to use ReactDOM.render() to update HTML instead of just writing the HTML directly?

I am new to learning react and am struggling to grasp why we need to utilize reactDOM.render() instead of just writing plain HTML code. ...

When utilizing webpack in Angular 5, the :host selector does not get converted into a component entity

Initially, I set up Angular with webpack following the configuration in this guide, including configuring webpack sass-loader. Everything was working smoothly until I encountered an issue today: app.component.ts @Component({ selector: 'ng-app&ap ...

Revamping elements according to ordered array. Angular version 4.3

Dealing with an array of data that needs to be sorted for displaying in a component seems to be a challenge. Despite having a functional code sample demonstrating the concept, the sorting is not reflected in the Angular app's DOM. The original data i ...

What is the best method for sending a document to a web API using Ajax, without relying on HTML user controls or forms?

I have successfully utilized the FormData api to upload documents asynchronously to a web api whenever there is a UI present for file uploading. However, I now face a situation where I need to upload a document based on a file path without relying on user ...