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

Using TypeScript for Routing in Angular

I encountered an error message that says the module 'route' is not available. I'm not sure why this is happening, any thoughts? "Uncaught Error: [$injector:nomod] Module 'route' is not available! You either misspelled the module n ...

In Angular, upon submitting the form, redirect to a separate component and display a message indicating the successful

I am facing an issue with a component that sends data to a service in order to save it in the database. Following this, I want to redirect or navigate to a different component and display a success message there. While I have implemented this functionality ...

An issue arises in Angular 17 where the view does not refresh when using setInterval, accompanied by the NG0500

I am currently working on a countdown timer feature where I want to continuously update the 'seconds' value in real-time, but I'm facing an issue with the UI not reflecting these updates. Whenever I attempt to implement this without utilizi ...

Search through an array of objects and assign a new value

I am facing a challenge with an array of objects structured as shown below: [ { "e_id": "1", "total": 0 }, { "e_id": "3", "total": 0 } ] My objecti ...

Has anyone tried integrating Reveal JS with Angular 8?

Encountering a problem with the integration of Reveal JS in an Angular component. Despite being initialized after DOM processing, it is not displaying anything. What is the appropriate time to initialize the reveal library? Is there a way to incorporate ...

Angular Dynamic Alert Service

Is it possible to customize the text in an Angular Alert service dynamically? I'm looking to make certain words bold, add new lines, and center specific parts of the text. Specifically, I want the word "success" to be bold and centered, while the rest ...

What is the best way to configure Apache to act as a reverse proxy for an ASP.NET Core API and an Angular

My aspnet core api is running on localhost:8080 (kestrel) and functions perfectly on localhost:80 (apache reverse proxy), making it accessible from the internet via www.example.com. I am looking to deploy my angular client on the same port locahost:80(www ...

Error: Missing provider for InjectionToken DEFAULT_LOCALE

I have been exploring the setup of an Angular 2 project with i18n. I followed a tutorial here that uses Transloco, and everything seemed to work perfectly. However, when running the unit tests, I encountered an error that I couldn't find any informati ...

Tips for changing a "raw" DOM Event into a React SyntheticEvent

Currently, I am working with two separate libraries. The first library emits "raw" DOM events (lib.dom.d.ts), while the other library consumes React.SyntheticEvents. I am seeking advice on the most efficient method to transform the raw event into a Synthe ...

Using custom types for prop passing in Next.js with TypeScript

After making a http call, I obtain an array containing JSON data. This data is then assigned to a type called Service. type Service = { id?: string; name?: string; description?: string; }; The api call is made in getServerSideProps and the Service type is ...

Eliminate redundant template code for Angular 2 components

Currently, I am developing a project using Angular 2 with the user-friendly Gentallela Alela HTML template. In many of my views, there are several components that share similar markup in their template files: <div class="col-md-12 col-sm-12 col-xs-12"& ...

Validation of Angular 5 forms for detecting duplicate words

I need help with a basic form that has one input. My requirement is to validate that the user does not input an existing word. If the user does enter an existing word, I would like to display a message below the input field saying "This word already exis ...

Get your hands on a complimentary Angular 2 scheduling tool

I am in need of integrating a scheduler into my angular 2 application. My goal is to schedule various employees within a day view and I found two paid components that might work for me: FullCalendar Scheduler Demo Bryntum Angular 2 Scheduler Currently, ...

The error page is requesting a root-layout, which indicates that having multiple root layouts is not feasible

My issue is as follows: The not-found page located in my app directory requires a root-layout which I have added to the same directory. However, this setup prevents me from using multiple root layouts in the structure below. How can I resolve this? It see ...

It is impossible to add a promise's value to an array

When attempting to push values into an array and return them, the console only displays an empty array or shows undefined! The issue seems to be that .then does not properly pass the value to the array. const net = require('net'); const find = re ...

Having trouble with assigning an error message in Formik validation using TypeScript

Currently, I am in the process of constructing a user input form in React & TypeScript using Formik. The form requires the user to input a name, email, and password. The input handling is functioning properly, but now I aim to implement validation functio ...

Adjust the properties within the component's styles using Angular 2

In this project, the objective is to dynamically change the background-color based on different routes. The goal is to display a specific color for UpcomingComponent while keeping the background-color consistent for all other routes. The approach involves ...

Angular issue: "anticipated to exit Angular zone, yet still found within"

I'm currently in the process of building a web application using Angular, and an error keeps appearing in the Chrome console: https://i.stack.imgur.com/sikuu.png Initially, I ignored the error as it didn't seem to impact the app's functiona ...

Revamping the static method signature of a class in Typescript

One of the modules I'm using is called some-module and it defines a class like this: export declare class Some<T> { ... static create<T>(): Some<T>; map<U>(x: U): Some<U>; } export default Some In my project, I ...

Utilizing Typescript: Ensuring an array includes only specified values from an enum through strict enforcement

In my Angular application, I have an HTTP service that returns the allowed accesses for a specific user. The response structure is as shown below:- { "accessId": 4209318492034, "folderPath": "data/sample_folder/", ...