Why am I only able to view private class properties in ngModel?

When utilizing a class with private properties in a model along with getter/setter methods, I am finding that I can only access the private property and not the public one using the getter/setter.

https://stackblitz.com/edit/angular-hx3t7g

Why is '_r' showing up in the ngModel instead of 'r'? How can I correct this, or should I even be approaching it this way?

Answer №1

This perfectly captures the essence of your TypeScript class known as Circle.

If you wish to access the r property of the Circle class, it seems unnecessary to also have a private property.

Your model should simply look like this:

export class Circle {
  C: number;
  r: number;
}

Answer №2

Based on my understanding, the calculation of the value for C is done when you are setting the value of r. Your code seems to be accurate, but it might be more effective if you make C a private variable since there is no need to directly access it as its value is determined by r.

export class Circle {
  private C: number; // change C to private
  private _r: number;

  set r(value: number) {
    this.C = 2 * 3.14 * value;
    this._r = value;
  }

  get r(): number {
    return this._r;
  }
}

By making _r private, external access is restricted, and the only way to modify it is through using r.

Answer №3

After searching, I finally found the solution I was seeking. For those who may be looking for the same thing, here is a helpful link: https://stackblitz.com/edit/angular-mkncdh

The trick is to define a private property and then set it to enumerable = false, as demonstrated in circle.ts. While it ended up being a bit more complex than expected, it achieves the desired outcome. Now, when you look at the output, you'll notice that only public properties are displayed in ngModel.

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

Issue with Angular Bootstrap 4 checkbox not updating as expected

I've been exploring some angular basics and encountered an issue with my checkbox list. Even though I've coded it to change when unchecked, it's not working as expected. Here's the snippet of my HTML component: <ul class="list-g ...

How to trigger a function in a separate component (Comp2) from the HTML of Comp1 using Angular 2

--- Component 1--------------- <div> <li><a href="#" (click)="getFactsCount()"> Instance 2 </a></li> However, the getFactsCount() function is located in another component. I am considering utilizing @output/emitter or some o ...

Guide to customizing action button color on MatSnackbar?

Currently, I am encountering an issue with the snackbar in my Angular 9 project. Despite adding CSS styles to both the component.scss file and the global style.scss file, the action button on the snackbar displays the same background and text color. In an ...

How to switch from Bootstrap 4 to Bootstrap 3.3 in an Angular project

As I embarked on my quest for answers, seeking out the necessary precautions for downgrading, I stumbled upon an interesting comparison. Bootstrap 3 boasts a 4-scope grid system, whereas Bootstrap 4 offers a 5-scope grid system. [xs,sm,md,lg,xl] The offs ...

Navigating to a new link containing query parameters

I am working on setting up a redirect in Angular 6 The process for the redirect is quite simple as outlined below: Obtain destination URL from parameters: this.returnUrl = this.route.snapshot.queryParams['route'] || '/'; Perform Red ...

How to use Angular 2 to communicate with JavaScript API whenever the router switches to

I am currently working on an Angular2 component that has a template which relies on JavaScript calls to load various components such as Facebook, Google Maps, and custom scripts. The necessary scripts are already loaded in the index.html file, so all I ne ...

A guide to transferring modules between component files in JavaScript

My query pertains to the handling of imports in web pages. When a file is imported into another, do the declarations and imports from the imported file become available in the file where it is being imported? A suggestion was made for me to import a compo ...

When attempting to assign a variable in my ngModel, what issue could be causing a problem?

I am facing an issue while trying to implement ngModel in my code. The code is not executing properly and an error is being displayed. Below is the code snippet: contact-form.component.html: <div class="container"> <form> <div class ...

Utilizing the power of HTML datalist and *ngFor to present one value while sending another

I am currently working on a project that involves creating an autocomplete text box using the <datalist> tag with *ngFor functionality. However, the code I am using is showing both the value declared with [value] and the input between the <option& ...

Guide on displaying the default null value button in Angular by implementing the *ngIf condition for the like, dislike, and default buttons within the *ngFor loop with Mysql and Node.js

Hey there, I'm currently working on creating a system for like, dislike, and default buttons similar to YouTube. However, I encountered an error when trying to implement the default button functionality. The first two conditions (like and dislike) are ...

Create a Typescript React class and define the State type as either an interface or null

When working with React and typescript, it is common to declare state types in the following manner: import * as React from "react" interface IState { someState: string } class MyClass extends React.Component<{}, IState> { state = { someSt ...

Utilizing React with Typescript to access specific props

I am a newcomer to React and TypeScript and I am facing a challenge while trying to enhance an existing component by creating a wrapper around it. The issue I am encountering is related to adding my custom values to the properties. My goal is to extend th ...

Using TypeScript to access native functions in react-native

Recently, I found myself in need of a basic function to interact with native code. Rather than creating a package, I opted to create the Java files as if they were for a plugin and registered them in MainApplication. As I'm using TypeScript, I am cur ...

Creating hierarchical TreeNode structure in TypeScript

As I work with a flat one-dimensional array of type TreeNode (view interface definition below), my goal is to recursively traverse the array and add subsequent array elements as children. While attempting a non-recursive approach using a buffer, I encount ...

Distinguishing the switch function from the React switch operator (jsx, tsx)

We are in the process of converting our portfolio from JavaScript to TypeScript, utilizing NextJS as the frontend framework and Strapi as the backend. To enable dynamic content, we have implemented a dynamiczone field within the post model that is accesse ...

TypeScript's robustly-typed rest parameters

Is there a way to properly define dynamic strongly typed rest parameters using TypeScript 3.2? Let's consider the following scenario: function execute<T, Params extends ICommandParametersMapping, Command extends keyof Params, Args extends Params[C ...

Avoid triggering the onClick event on specific elements in React by utilizing event delegation or conditional rendering

programming environment react.js typescript next.js How can I prevent the onClick process from being triggered when the span tag is pressed? What is the best approach? return ( <div className="padding-16 flex gap-5 flex-container" ...

Task ':processDebugGoogleServices' could not be added because there is already a task with the same name

Trying to test out the firebase FCM plugin, but encountering numerous errors along the way. After resolving most of them, I attempted to perform the following command: ionic cordova build android, only to be faced with the following error: Here's wha ...

React application problem detected

Facing a challenge with generating a new Date object in React while using a specific date string format YYYY-MM-DD, without being affected by my machine's timezone. This issue is causing problems for a function that depends on the accurate date. Here ...

Designing a Bootstrap table using various Angular elements

I'm currently in the process of developing a Bootstrap table that utilizes two Angular components. This is how my code is structured : componentA.html <table class="table table-bordered"> <thead> <tr> <th scope="col">#</ ...