Adjusting the maximum value in the Angular Bootstrap UI rating system using ng-model

I have implemented a rating system using Angular-UI. The number of stars displayed is determined by a variable named max. While I am able to display this variable within an input field using ng-model, any modifications made to it do not reflect in the number of stars displayed.

You can observe this behavior in the following Plunker.

Below is the pertinent javascript code:

.directive('myRating', function() {
  return {
    restrict: 'E',
    template: '<div> \
         <div class="row rating" ng-controller="RatingDemoCtrl"> \
           <rating value="rate" max="max" readonly="isReadonly" state-on="\'glyphicon-star rated\'" state-off="\'glyphicon-star\'"></rating> <a class="glyphicon glyphicon-pencil" ng-show="editMode" ng-click="editStars = !editStars"></a>\
            <input ng-if="editStars" type="text" class="form-control" ng-model="max" /> \
         </div> \
      </div>',
    replace: true,
  };
});

var RatingDemoCtrl = function ($scope) {
  $scope.rate = 0;
  $scope.max = 10;
  $scope.isReadonly = false;

  $scope.hoveringOver = function(value) {
    $scope.overStar = value;
    $scope.percent = 100 * (value / $scope.max);
  };
};

Although the ng-model successfully displays the value of max each time, real-time modifications are not reflected. Any suggestions on how to address this issue?

Answer №1

If you're looking to customize the ui-bootstrap code, it's definitely possible. I made some tweaks to the code and it seems to be working perfectly fine. Take a look at the updated PLUNKER to see the changes in action (lines 2493-2559).

First, I added a two-way bound object called maxRange in the directive:

scope: {
  value: '=',
  onHover: '&',
  onLeave: '&',
  maxRange: '=max'
},

Then, in the controller, I adjusted the code to watch for changes in the maxRange value and update the objects accordingly:

$scope.$watch('maxRange', function() {
  $scope.range = createRateObjects(new Array(parseInt($scope.maxRange)));
});

Sometimes a little bit of hacking is necessary. Feel free to make modifications to code if it doesn't quite meet your requirements!

Answer №2

After a brief inspection of the issue, it appears that the AngularJS Bootstrap Rating directive does not support real-time updates for the maximum value.

Answer №3

By sheer chance, I stumbled upon another clever workaround:

If you enclose your rating directive within an ng-if statement, it also seems to do the trick.

<div ng-if="true">
  <rating max="myVar"></rating>
</div>

As per the Angular documentation, ng-if will reload the HTML every time it's evaluated, prompting a reassessment of myVar.

While it may still be considered somewhat unconventional, I find this method more aesthetically pleasing than altering code within your bower_components directory, which gets overwritten with each update.

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

I'm thinking about transitioning my current React app to Next.js. Do you think it's a good move?

We have recently transitioned our project from react-redux with firebase authentication and solr DB to next.js. Below is the updated folder structure of our app: --src --actions --assets --components --controllers --firebase --redu ...

Incorporating lodash into Angularjs for enhanced functionality

After stumbling upon an app online that utilizes AngularJS with Lodash, I noticed a simple way in which Lodash is incorporated. By including the following line in the body (after angular has been included): <script src='vendor/lodash/3.3.1/lodash. ...

The canvas could not be loaded properly in THREE.Texture()

I'm having trouble with adding an image onto the side of a cube that I'm creating. The image loads onto the canvas, but I'm struggling to incorporate it into the texture. function createPictureCanvas(text, font, foreground, background, xres ...

Having trouble with uploading images using Form.File in React?

I'm facing an issue with my React code for uploading an image. It doesn't seem to be working and I can't figure out why. Can someone please assist me? return ( <div> <FormContainer> <h1>Edit Product& ...

What method would you recommend for distributing static and configuration data within a Laravel/Vue.js project?

Working on a Laravel/Vue.js project is posing a challenge of using the same data, such as a static list of Operating Systems, in both the backend and frontend. Considered options include: Duplicating data in both Laravel and vue.js projects Loading the d ...

The React Route Component is responsible for managing all routes that start with "/", with the exception of the "/login" route

When accessing the /login route, the Dashboard component is also rendered. However, I need to exclude the Dashboard component while on the Login page. The exact attribute isn't suitable in this case because the Dashboard has nested paths such as /das ...

How to Boost SEO with AngularJS by Customizing Meta Descriptions for Every Partial

Imagine a scenario where there are two partial pages - Sign Up ('/signup') and Sign In ('/signin'). For the sign up partial, I want to include this meta tag: <meta name="description" content="Sign Up"> Similarly, for the sign i ...

$scope disappears following asynchronous call

In a previous project, I had this code working flawlessly. However, when I transferred the code to a new project, even the simplest case is failing. This is what my controller looks like: angular.module('myApp.controllers'). controller(' ...

Use JavaScript to switch the h1 title when selecting an option from the dropdown menu

As a beginner in coding, I am struggling to find a solution using an if statement for this particular problem. Although I can achieve the desired result with HTML code and options, this time I need to use arrays and an if function. My goal is to create a ...

Having trouble locating the name 'angular' in TypeScript error message

I have completed all the necessary settings, such as adding the typescript compiler in webstorm and installing tsd with npm. However, I am still encountering an error stating 'Cannot find name Angular'. tsd.json { "version": "v4", "repo": ...

Deleting an List Item from an Unordered List using Angular

Within my MVC controller, I have an unordered list with a button on each item that allows the user to remove it from the list. The issue I'm facing is that while the row is deleted from the database, it still remains visible on the screen. This is ho ...

The code is encountering an error stating 'controller with the name of 'Aditya' is not found in the registry'

HTML Code: <div class="container" ng-controller="example"></div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <script src="scripts/controller.js"></script> Include the fo ...

The Angular bootstrap popover vanishes as soon as the mouse hovers over it

Currently, I am facing an issue with an angular bootstrap popover on some text. The problem arises when the user tries to click on a link inside the popover as it disappears. Additionally, when changing from one popover to another, the previous one does no ...

The function of removing the ng-submitted class in AngularJS after form submission seems to be malfunctioning when using setPristine and setUnt

When a form is submitted in Angular, the class ng-submitted is automatically added by default. In my attempts to reset the form state by using $setPrestine and $setUntouched, I encountered some errors that prevented it from working properly. Examples of t ...

Error: The 'current' property is not defined and cannot be focused on

I have a total of 4 input fields, and I would like the user to automatically move to the next input field after filling out the current one. constructor(props) { ... this.textInput1 = React.createRef(); this.textInput2 = React.createRef(); ...

The deployment of the Fire-base Cloud Function was successful and error-free. However, the function does not appear to exist in the Firebase system

After deploying a JavaScript Cloud Function, it shows that the deployment is completed. However, when I check Firebase, the function is not there. Oddly, TypeScript Cloud Functions deploy successfully. I followed all the steps outlined in the original Fir ...

Search for text in multiple tables using jQuery and automatically trigger a button click event when the text is found

I am attempting to query certain tables and click a button within a cell of a specific table. Below is the code I am currently using: links[1].click(); iimPlayCode('WAIT SECONDS = 2') var compTabs = window.content.document.getElementById(' ...

Utilize text-to-speech technology to convert the output results into

Looking for a way to live stream a text to speech message triggered by a button press on your website? Unsure how to accomplish this with Node.js and generate the file after the button press? Let's explore some solutions together. ...

What could be preventing my PHP function from being executed when the button is clicked?

My PHP block has all the working get functions as expected. <?php function getTitle() { $connection = new PDO('mysql:host=x.x.x.x;dbname=x;charset=utf8', 'x', 'xxxx'); $query = $connection->query("SELE ...

Extracting Hidden Links: Retrieve the URL that is exclusively visible on the webpage, but not in the source code

While browsing a website, I came across a link that I want to access. However, the link is displayed on the website but is not visible in the HTML file. There is a copy button that allows the link to be copied to the clipboard. I am wondering if anyone k ...