Changing the ngModel value within ngFor loop

I am working on a project where I need to display a list of grades from an object called 'grades'. Additionally, I want to integrate a slider component for each grade, with the value of the slider corresponding to a predefined list. However, it seems that assigning a function to [(ngModel)] is not as straightforward. The approach of [(ngModel)]=getGrades(i) does not yield the desired result. What would be the most effective way to achieve this?

<div *ngFor= "let item of grades; index as i">
    <h6> Grades: {{item}} </h6>
    <slider [(ngModel)]=getGrades(i)></slider> <--this doesn't work
</div>
myGrades: SliderValue;
getGrades(i) {
    let sliderGrades = [70, 20, 10]
    this.myGrades = [parseInt(sliderGrades[i].toString())];
    return this.myGrades;
}

The array 'sliderGrades' will have a set number of values which need to be assigned accordingly to the top three grades. For instance, if the grades range from A to F: it should appear as follows in the HTML:

A  slider 70
B  slider 20
C  slider 10
D  slider 0
E  slider 0
F  slider 0

Answer №1

Experiment with square brackets exclusively.

<slider [ngModel]=getGrades(id)></slider> 

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

Selecting a single mat radio button from a group of radio buttons within a column in a Mat Table

How can I ensure that only one radio button is selected when setting the primary contact detail and saving it, as shown in (image 1)? https://i.sstatic.net/hWXK8.png After clicking the Save button, how do I display the saved details as shown in (image 2)? ...

Accessing the total number of items from a service in Angular

I am facing an issue with my cart-service and 2 components. The product-list component is responsible for displaying the products along with a buy button. However, the top-bar component seems to have trouble displaying the count when I try to directly call ...

Converting TypeScript to JavaScript: A Step-by-Step Guide

I have this code written in Typescript and I need to convert it to JavaScript const Home = (props) => { return ( <div> {props.name ? 'Hi ' + props.name : 'You are not logged in'} </div> ); }; How can I re ...

Steps for customizing the default properties of a material ui component

Is there a way to change the style properties listed on the main element? height: 0.01em; display: flex; max-height: 2em; align-items: center; white-space: nowrap; } <InputAdornment position="end" > {"hello& ...

Nested function TypeScript declarations

Currently, I am attempting to define a type for my controller function in (nodejs) similar to the following export const registerUser = asyncWrap(async function(req:Request, res:Response, next:NextFunction) { res.status(200).json({ success: true}); }) ...

What to do when the 'image' property in Next.js next/image has an implicit 'any' type and needs fixing?

I'm a newcomer to Next.js and TypeScript and I can't seem to find any helpful resources on resolving this particular issue: import Image from 'next/image'; export default function Item({ image }) { // <-- parameter image needs a &ap ...

Utilizing ngFor to generate buttons that are disabled based on specific conditions

I need assistance with creating a dynamic list of buttons from an array using ngFor. While I have achieved this, my current issue lies in disabling the buttons once they exceed a certain "number" representing the user's level. Here is an example of w ...

Exploring the process of enabling full text search on a multi-page encrypted document

I am faced with the task of managing hundreds of documents, each consisting of multiple pages. Users need the ability to search within a document for specific words or sentences. My goal is to retrieve all files that contain the searched text. Currently, ...

The input elements fail to register the passed-in value until they are clicked on

I am experiencing an issue with my form element that contains a few input fields. Two of these inputs are set to readOnly and have values passed in from a calendar element. Even though the input elements contain valid dates, they still display an error mes ...

The type string[] cannot be assigned to type 'IntrinsicAttributes & string[]'

I'm attempting to pass the prop of todos just like in this codesandbox, but encountering an error: Type '{ todos: string[]; }' is not assignable to type 'IntrinsicAttributes & string[]'. Property 'todos' does not ex ...

Update the UI with changes from the BehaviorSubject after receiving the HttpClient response

I am currently working on a small service that makes a POST request to the backend. The response from this POST request should be added to a BehaviorSubject which is then used in various parts of the Angular application. While I can see a new record being ...

Adding a local image to Firebase Storage in Angular5 / Ionic3

Uploading images is a breeze using the following method (select input file): import { AngularFireStorage } from 'angularfire2/storage'; @Component({ selector: 'app-root', template: '<div>' + '<input c ...

The error message "Theme does not include property 'navHeight'" is indicating that the 'navHeight' property is not defined within the 'Theme' type. This issue occurs when using MUI v5 syntax with Types

When attempting to incorporate MUI with new props declared in the Interface inside the theme.ts file (as suggested by the MUI docs), I encountered the error mentioned above while theme.palette.primary.main does work. Despite trying various solutions like i ...

Tips for using CanActivate in Angular 5 with angular2-jwt

I am in the process of incorporating the canActivate class to manage URL access. To handle token loading, I have created the following functions: saveToken(jwt:string){ this.jwtToken = jwt; localStorage.setItem('token',jwt); let jwtH ...

Surprising literal found at the second position

Encountered an error while trying to display a date as an array on an HTML page. The current implementation is causing an issue in the p-calendar tag within ngmodel, where date2[i] is being displayed based on the index i derived from p-datalist. I am retur ...

Filtering Deno tests by filename: A step-by-step guide

How can I selectively run Deno tests based on their filenames? Consider the following test files: number_1_test.ts number_2_test.ts string_test.ts If I want to only run tests with filenames starting with number*, I am unable to use either of these comma ...

What is the best way to set up an endpoint in Angular for image uploading?

Using the Kolkov Angular editor in my Angular application, I have successfully created a rich text editor. Currently, I am looking to upload images from the editor to the server. I already have a function in place that takes a file as an argument and send ...

Getting Session from Next-Auth in API Route: A Step-by-Step Guide

When printing my session from Next Auth in a component like this, I can easily see all of its data. const session = useSession(); // ... <p>{JSON.stringify(session)}</p> I am facing an issue where I need to access the content of the session i ...

Discover the method for converting a local file into a string

Looking to retrieve and read an SVG file in Angular 6 from the assets folder as a string. I've attempted the following: this._htmlClient.get('../../assets/images/chart1.svg').subscribe(data => { console.log('svg-file: ', ...

Implementing a Typescript directive in AngularJS to create a class

After using AngularJS for quite some time, I decided to explore Typescript. I managed to convert most of my Angular code to Typescript and found it beneficial, especially when working with services. However, I am struggling to convert the following direc ...