Error in AngularJS ng-repeat syntax

As a newcomer to AngularJS, I ventured into creating a Bootstrap form with a loop but encountered an error. What could be the mistake I made?

<form class="form-horizontal" role="form" name="newForm" novalidate ng-controller="newFormController">
<div class="form-group" ng-class="{'has-error': newForm.{{field.name}}.$invalid && !newForm.{{field.name}}.$pristine}" ng-repeat="field in fields">
    <label class="col-sm-2 control-label">{{field.label}}</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" ng-model="{{field.name}}" name="{{field.name}}" {{field.validation}} placeholder="{{field.placeholder}}" />
        <p ng-show="newForm.{{field.name}}.$invalid && !newForm.{{field.name}}.$pristine" class="help-block">{{field.error}}</p>
    </div>
</div>
</form>

The issue at hand:

Syntax Error: Token 'field.name' is unexpected, expecting [:] at column 3 of the expression [{{field.name}}] starting at [field.name}}].

This occurs during parsing:

ng-model="{{field.name}}"

Here's the controller code:

function newFormController($scope) {
$scope.fields = [{
    name: 'Firstname',
    label: Label.Get('28076475'), //First Name
    placeholder: Label.Get('44175380'), //Enter First Name
    validation: 'required',
    error: 'Firstname is required.'
}, {
    name: 'Lastname',
    label: Label.Get('28076500'), //Last Name
    placeholder: Label.Get('44175365'), //Enter Last Name
    validation: 'required',
    error: 'Lastname is required.'
}];
}

Answer №1

It seems like there might be an issue with ng-model.

To resolve it, simply update the code from

ng-model="{{field.name}}"

to

 ng-model="field.name"

Answer №2

It seems like you are attempting to assign the value of field.name as a modal. To accomplish this, replace newForm.{{field.name}} with newForm[field.name] in ng-model and other ng directives.

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

Obtaining the innerHTML value using JavaScript in an Asp.net Core 5 View

Apologies for any inaccuracies in my English writing. In the Asp.Net Core project view, I am trying to use JavaScript to capture multiple Span tags within innerHTML represented by a loop and display their sum in another tag. However, I am only able to ret ...

Angular - Javascript - Oops! The variable 'google' seems to have gone missing after reloading the page

For my website, I utilized Angular 2+ and integrated the Google Maps Api by adding the following code to my index.html file: <script async defer src="//maps.googleapis.com/maps/api/js?[myKey]&libraries=places"> </script> ...

javascript if an error occurs, refresh the webpage

Currently, I am inquiring about the most effective method for managing JavaScript errors. Given that my application relies heavily on JavaScript, despite diligent testing efforts, encountering bugs is almost certain. I'm interested in knowing if ther ...

Troubleshooting your Meteor App on Nitrous.io with server-side debugging

I currently have a basic Meteor application up and running on a Nitrous box. I am interested in utilizing node-inspector for debugging the server-side part of my app, but I'm facing difficulty accessing the console as detailed here. The Meteor app is ...

The Access-Control-Allow-Headers does not permit the use of the Authentication request header field

I've noticed this question frequently popping up on Stack Overflow without a clear explanation of its significance. Could someone kindly elaborate on the meaning of this error? Request header field Authentication is not allowed by Access-Control-Allo ...

Importing the isPropertyUpdated method in Angular 4

My challenge lies in utilizing the isPropertyUpdated function within Angular 4. However, I have encountered a roadblock as Angular 4 does not facilitate deep imports. An example of an import that fails to work on Angular 4 is: import {isPropertyUpdated} ...

When trying to run ionic serve, I encountered the following error: "[ERROR] ng has unexpectedly closed with an exit code of 127."

My attempt to launch an ionic app on my Mac has hit a roadblock. While running npm install for the dependencies, everything goes smoothly without any issues. However, when I try to run 'ionic serve' or 'ionic s', an error crops up: [ng] ...

Is there a way to display multiple images in a JSON message object?

If you're looking for a fun way to generate random dog images, then check out my DogAPI image generator! Simply enter a number between 1-50 into the form text box, hit send, and watch as it displays that amount of random dog photos. I'm almost t ...

How to Send Data to AngularJs Controller via Parameters

Starting out with AngularJs and trying to figure things out. On my details page, I am displaying a link as shown below: <a href='/device-summary/"+ full.link.href+"'>"+ data_to_be_displayed+"</a> In the provided link, full.link.href ...

What is the method for executing a function enclosed within a variable?

As someone new to the world of Java, I have encountered a puzzling issue with some code related to a game. Specifically, there seems to be an obstacle when it comes to utilizing the navigator function. When I click on this function in the game, some sort o ...

Loop through the JSON data and dynamically display corresponding HTML code based on the data

I've developed a survey application using React, where I initially wrote code for each question to display radio buttons/inputs for answers. However, as the survey grew with multiple questions, this approach resulted in lengthy and repetitive code. To ...

Exploring the latest updates in MUI modern version

The MUI documentation suggests using a modern folder with components designed for modern browsers. Is there a way to configure webpack to automatically rewrite imports like import {Box} from "@mui/material" to use the modern version without manually changi ...

What is the default method for automatically disabling drop down fields?

I've created a script that will gray out a drop-down field if a prior selection wasn't made. Once the appropriate choice is selected, the grayed-out field will become active. My query is, how can I set the drop-down field to be initially grayed ...

No instances are returned by Processing.instances

I am experiencing an issue with a webpage that is running 2 processing sketches. I came across a suggestion to call Processing.instances[0].exit() in response to a question on dynamically unloading a Processing JS sketch from canvas. However, when I attem ...

What would be the optimal type for the second argument of the `simulate` method?

When using the simulate function, I am familiar with code like this: simulate("change", { target: { value: '7' } }); However, if my onChange function requires an object as a parameter, what should I pass in the second argument? interface myObj ...

A guide on implementing multiline properties in a property file for Selenium WebDriver

At the moment, I am focusing on utilizing Selenium WebDriver with code that is developed in Java. In the snippet below, I have successfully verified if the dropdown values match the UI for one dropdown. Now, I aim to extend this capability to check multip ...

Undefined scope

angular.module('CrudApp', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/', { templateUrl: 'assets/tpl/lists.html', controller: ListCtrl }). when('/add-user&apos ...

React hook form submit not being triggered

import React, { useState } from "react"; import FileBase64 from "react-file-base64"; import { useDispatch } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import { TextField, Select, Input ...

Validate the date selected in a dropdown menu using JavaScript

I'm still relatively new to Javascript, just working my way through some tutorials. I have three select boxes in my HTML form as shown below. HTML Form: <table> <form id="enrolment" name="enrolment" onsubmit="return datevalidate();" action ...

Automate your workflow with Apps Script: Save time by appending a row and seamlessly including additional details to the

I currently have 2 server-side scripts that handle data from an html form. The first script saves user input to the last row available in my Google sheet, while the second script adds additional details to the newly created row. Although both scripts work ...