Simple yet perplexing JavaScript within an Angular directive

In the tutorial, the author explains that each element in the stars array contains an object with a 'filled' value, which is determined as true or false based on the scope.ratingValue received from the DOM.

directive('fundooRating', function () {
  return {
    restrict: 'A',
    template: '<ul class="rating">' +
              '<li ng-repeat="star in stars" class="filled">' +
                  '\u2605' +
              '</li>' +
            '</ul>',
    scope: {
      ratingValue: '='
    },
    link: function (scope, elem, attrs) {
      scope.stars = [];
      for(var i = 0; i < scope.max; i++) {
      scope.stars.push({filled: i < scope.ratingValue});
  }
} 

As someone new to JavaScript, understanding the last line of code can be challenging. The term 'filled' only appears in the CSS file and the provided template, making it difficult to comprehend the boolean value behind it. The syntax may seem confusing at first glance. If you are facing similar difficulties, check out the GitHub code for more insights.

Answer №1

After reviewing the source code, I found that the template includes the following snippet:

'<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)">'

According to the documentation on ng-class, when an object is provided to the directive, it iterates through each property and adds the property name as a classname if the value is truthy. For example:

ng-class="{filled:true, bold:false}"
will result in class="filled".

Therefore, this line of code determines the 'filled' status of the star, applying the filled class to only the elements up to the max scope index.

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

Is it possible to remove a particular div after clicking the delete button, especially if multiple divs are displayed upon clicking the add

var count = 2; var countMax = 5; function adddt() { if (count > countMax) return; document.getElementById('dt-' + count + '').style.display = 'block'; count++; } <link href="https://maxcdn.bootstrapcdn.com/bo ...

Issue with Angular 2 (Ionic 2): Struggling to properly display Component within Page

I have successfully created an Angular 2 Component: import {Component, View} from 'angular2/core'; @Component({ selector: 'sidemenu' }) @View({ templateUrl: 'build/pages/menu/menu.html', }) export class Menu { } Howev ...

Retrieve files from Amazon S3 using JavaScript

I'm currently working with a javascript file that's intended to download a text file from one of my S3 buckets. However, after running this file using "node file.js", nothing happens and there's no output. Is there something I'm missing ...

Exploring the World with GPS Technology and Coding

Starting off, I am looking to develop a web-based or browser-based application in the near future. The goal is to incorporate a GPS module as part of the interface for a self-hosted application on tablets or laptops, utilizing the data for tracking purpo ...

Encountered an issue when executing "npm start": The system does not recognize 'next' as a valid internal or external command

Encountering an issue when running the "npm start" command in the terminal while attempting to create a cryptocurrency tracker using React.js Displayed contents of my package.json file: { "name": "nextjs-crypto-api", "version ...

Is it possible to perform cross domain AJAX calls while implementing Basic Authentication?

Having a ColdFusion application located behind an ISA Server presents some challenges. In this case, part of the application requires Basic Authentication while another part does not. The issue arises when trying to access cookies set by the ISA Server upo ...

Troubleshooting: jQuery script encountering issues with loading Bodymovin JSON files

The animations for the slider and shuffle lottie are designed to go from 0 to 100 and then back to 0 when toggled, similar to the box animation. However, it appears that the slider animation disappears in the final frame while the shuffle animation appear ...

Can anyone recommend a speedy sorting algorithm for an extensive list of objects in JavaScript?

Struggling to organize a large array of 2000 elements in ReactJS using JavaScript. The array includes: data = [ { index: 0, id: "404449", product_name: "ette", brand_name: "Dyrberg/Kern", base_pri ...

Activate a module within a Vuex action only upon dispatching

Is there a way to import a package for use in a Vuex Action only when the Action is dispatched, in order to reduce the size of my entry bundle? Here's what I've tried: export const actions = { async createNewBin(store, bin) { const fireba ...

jQuery still not running despite using .ready

I have been attempting to run some jQuery code on my HTML page. After doing some research, I learned that using .ready may be necessary to ensure the DOM is fully loaded before executing the script. Despite this, I am still having trouble getting the scrip ...

The app's connection issue persists as the SDK initialization has exceeded the time limit

We are currently facing an issue when trying to publish a new manifest for our app in the store. The Microsoft team in India is encountering an error message that says "There is a problem reaching the app" during validation. It's worth noting that th ...

Resolving the Persistence Problem of Navigation Bar Fading In and Out

I've been struggling for hours trying different methods to achieve the desired outcome, but unfortunately, none of them have worked as expected. My goal is simple: I want the fixedbar to fade in when the scroll position exceeds the position of the ph ...

Error: Unable to execute decodeHtml because it is not recognized as a function

After transitioning to VueJS 2, I encountered a challenge. While using a filter that calls a custom function, I received the error message: TypeError: this.decodeHtml is not a function Below is my code snippet: new Vue({ el: '#modal' ...

Get selectize.js to display only options that begin with the user's input

Using selectize.js, my current setup looks like this: Instead of only showing words that start with 'arm', it displays words or options containing 'arm' as a substring elsewhere. I want to modify the function to display only words or ...

Instructions on setting div opacity when Overflow is set to visibleExplanation on setting opacity for div with Overflow

In my HTML code, I have a div element and an image tag stacked one on top of the other. The div is smaller than the image. I have set the overflow property to visible for the div. My query is, when I add an image to the image tag, the content that overflow ...

Asynchronously retrieving results in MongoDB

My task involves fetching all users from the users collection. app.post('/login', function(req,res,next){ users = self._db.get('users', {}) }) Below is the function in my database class: this.get = function( col, opt ) { ...

Help kids find solutions

Here is the HTML code I am working with: <div class = "touch" onclick="do(this)"> <span>text01</span> <span>text02</span> <span>text03</span> <span>text04</span> <div class = "findMe">a ...

Removing one element from an array with mongoose

As a newcomer to web development, I am currently working on creating a todo app. Below is the schema and model that I have: const tdSchema = new mongoose.Schema({ category: { type: String, required: true, unique: true }, tds: { type: ...

Create a visual layout showcasing a unique combination of images and text using div elements in

I created a div table with 3 images and text for each row, but I'm struggling to make all the text-containing divs uniform in size. How can I achieve this? Also, I need to center this table on the screen, and currently using padding to do so. Is there ...

Iterating through a JSON object to verify the presence of a specific value

I have a JSON Object and I am looking for a way in Angular 6 to search for the value "Tennis" in the key "name". Can you provide guidance on how to achieve this? { "id":2, "name":"Sports", "url":"/sports" "children":[ { "id":1, ...