Encountering a Difficulty while attempting to Distinguish in Angular

I am currently working on a form where I need to dynamically add controls using reactiveForms.

One specific task involves populating a dropdown menu. To achieve this, I am utilizing formArray as the fields are dynamic.

Data:

{
  "ruleName": "",
  "ruleDescription": "",
  "ruleOutcome": "",
  "addVariable": "2",
  "variables": [
    {
      "id": "2",
      "name": "Device Trust Score",
      "operator": [
        {
          "name": "Greater Than <",
          "id": 3
        },
        {
          "name": "Less Than >",
          "id": 4
        }
      ],
      "values": ""
    }
  ]
}

HTML:

<tbody formArrayName="variables">
   <tr *ngFor="let variable of addRuleForm.get('variables').controls; let i=index" [formGroup]="variable">
   <td>{{ addRuleForm.controls.variables.controls[i].controls.name.value}}
      <input type="hidden" formControlName="id" value="addRuleForm.controls.variables.controls[i].controls.id.value" [attr.id]="'id'+i">
   </td>
   <td>
      <select class="form-control input-sm" formControlName="operator" [attr.id]="'operator'+i">
         <option value="">Select an Operator</option>
         <option *ngFor="let o of addRuleForm.controls.variables.controls[i].controls.operator.value" value="{{ o.id }}">{{ o.name }}</option>
      </select>
   </td>
   <td>
      <button type="button" class="btn btn-danger btn-sm" (click)="removeVariable(v.id)"><i class="fa fa-trash-o"></i>
      </button>
   </td>
   </tr>
</tbody>

The select input is displayed correctly on the page and the ID value is set correctly when inspected in the source code.

However, upon selecting an option from the dropdown, I encounter the following error:

Error: Error trying to diff '4'. Only arrays and iterables are allowed
, specifically related to the selected item's ID.

Any suggestions?

Answer №1

It seems like the message is indicating that you are attempting to loop through something with *ngFor that it cannot interpret.

My guess is that this

addRuleForm.controls.variables.controls[i].controls.operator.value
could possibly be 4? Were you intending to utilize the value attribute in this instance?

If you wish to iterate over a JSON object, you can utilize Object.keys. There are numerous instances available here: access key and value of object using *ngFor

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

The video element in my Angular application is not functioning properly in Safari

I am having an issue with playing a video in my angular app. It works perfectly in all browsers except for Safari : <video controls autoplay muted class="media"> <source src="https://my-site/files/video.mp4" type="video/mp4" /> </video& ...

Determining when to include @types/packagename in React Native's dev dependencies for a specific package

Just getting started with React Native using typescript. Take the package vector icon for example, we need to include 2 dependencies: 1. "react-native-vector-icons": "^7.1.0" (as a dependency) 2. "@types/react-native-vector-icons": "^6.4.6" (as a dev ...

Save data to local storage when the form is submitted, retrieve it when the page is reloaded

I need help with setting form data in local storage upon form submission and displaying a message in the console if the form has already been submitted when the page is refreshed. I am struggling to write the reload condition for this functionality. Here ...

Navigating a page without embedding the URL in react-router-dom

In my application, I am utilizing react-router-dom v5 for routing purposes. Occasionally, I come across routes similar to the following: checkup/step-1/:id checkup/step-2/:id checkup/step-3/:id For instance, when I find myself at checkup/step-1/:id, I int ...

What is the correct way to trigger an event specified as a string parameter in the emit() function?

My current goal is to pass the emit name as a string (for example, 'showComponent') from child to parent. I then want to trigger another emit in the emitAction(callbackName: string) function, and finally execute the showComponent() function. I&a ...

The search for 'partition' in 'rxjs' did not yield any results

Recently, I attempted to incorporate ng-http-loader into my Angular project. After successfully installing the ng-http-loader package, I encountered an error during compilation. The specific error message displayed was: export 'partition' was ...

Storing data retrieved from a GraphQL response into the sessionStorage: A step-by-step guide

I am facing a challenge in saving my GraphQL response in sessionStorage to access it across different parts of the application without making repeated API calls. I am currently using the useQuery hook with a skip property to check if the data is already st ...

Issues with binding Angular reactive forms

Exploring the construction of nested reactive forms: https://stackblitz.com/edit/angular-mgrfbj The project structure is as follows: ---create company form (hello.component.ts) --- company details form (company-details.component.ts) --- [ ...

What is the process for generating an index.d.ts file within a yarn package?

I'm facing an issue with creating the index.d.ts file for my yarn package. Here is my configuration in tsconfig.json: { "include": ["src/**/*"], "exclude": ["node_modules", "**/*.spec.ts"], " ...

I need to access the link_id value from this specific actionid and then execute the corresponding function within the Ionic framework

I have a JavaScript code in my TypeScript file. It retrieves the attribute from a span element when it is clicked. I want to store this attribute's value in a TypeScript variable and then call a TypeScript function. Take a look at my ngOnInit method, ...

Angular 2 is not compatible with custom reuse strategy when implementing lazy loading for modules

I attempted to implement a custom route reuse strategy in my angular2 project, but I encountered issues with lazy loading of modules. Does anyone have insight on this matter? My project is using angular 2.6.4. Here is the code for the custom route-reuse-s ...

Encountering an issue with resolving parameters for the DecimalPipe in ngBootstrap/Angular2

Just delving into the world of Angular2 and trying to learn through hands-on experience. However, I've hit a roadblock! I attempted to import ng-bootstrap and encountered this error: https://i.stack.imgur.com/QDVJ3.png Here's my systemjs.config ...

Changing the content of an HTTP response with the help of RXJS

My API response includes a payload with various details about the queue and its customers. Here is an example of the payload: { "uid": "string", "queue": { "size": 0, "averageWait ...

Is it possible to reset the attributes of a container's children using a renderer?

Within a container, there are dropdowns, multiple selects, and quantity selections. When a button is clicked, I aim to reset the state of the component. https://i.stack.imgur.com/TY5un.png <input #select type="checkbox" value="somevalue"/> The ...

Issue encountered when attempting to assign `fontWeight` within `makeStyles` using `theme` in Typescript: Error message states that the property is not

Currently, within my NextJS project and utilizing MUI, I am attempting to define a fontWeight property using the theme settings in the makeStyles function. Oddly enough, this issue only arises when building inside a docker container, as building locally po ...

Utilizing Dynamic Variables in Angular 4 Templates

Hey everyone, I recently created an Angular 4 app and developed a component called Contentholder. The Contentholder consists of HTML & CSS to display the content holder that I am utilizing. When I include <content-holder></content-holder>, ...

Retrieving chosen items from dynamically generated checkboxes using Angular 2+

I have a piece of HTML code where I am trying to capture the value of all selected checkboxes whenever any checkbox is checked or unchecked, triggering the (change) event. <div class="checkbox checkbox-primary" *ngFor="let category of categories; let i ...

The error message "Property 'then' is not available on type 'void' within Ionic 2" is displayed

When retrieving data from the Google API within the function of the details.ts file, I have set up a service as shown below. However, I am encountering a Typescript error stating Property 'then' does not exist on type 'void'. this.type ...

The Angular dependency provider is failing to supply the requested alternative class

I am currently in the process of writing unit tests for the doc-manager.component component. This particular component relies on the DocService, but I want to swap it with instances of MockedDocService within my tests. By leveraging alternative class prov ...

Is it possible for React props to include bespoke classes?

In our code, we have a TypeScript class that handles a variety of parameters including type, default/min/max values, and descriptions. This class is utilized in multiple parts of our application. As we switch to using React for our GUI development, one of ...