Retrieving Items from ngRepeat Iterator

Is there a way for me to retrieve a specific record within an ngRepeat loop from the controller, particularly for form validation purposes?

Here is a form where users can dynamically add or remove records:

<table class="table table-condensed span12" id="tableParticipants">
  <!-- snip -->
  <tbody>
    <tr ng-repeat="person in people">
      <td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td>
      <td><input type="text" data-provide="typeahead"  placeholder="Firstname Lastname" ng-model="person.name"></td>
      <td><input type="text" placeholder="Title" ng-model="person.title"></td>
      <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i></td>
    </tr>
  </tbody>
</table>

During form submission, I need to validate that all employee IDs are correct to avoid any database errors. Therefore, as I iterate through the loop:

$scope.people.forEach(function(element, index, array)
{
  if ($element['empid'] == BAD)
  {
    // flag the corresponding ngRepeat row as erroneous
  }
}

What is the best approach to access a specific row for a particular record in this scenario?

Answer №1

If you want to customize the appearance and behavior of your array items in Angular, you can easily do so by adding a CSS class name or an error message to each item. Angular will then take care of updating the view accordingly. You have the flexibility to show/hide messages, add classes, and more.

<tr ng-repeat="person in people" ng-class="person.error">
 <td><input type="text" pattern="[0-9]*" data-provide="typeahead" placeholder="8675309" ng-model="person.empid"></td>
  <td><input type="text" data-provide="typeahead"  placeholder="Firstname Lastname" ng-model="person.name"></td>
  <td><input type="text" placeholder="Title" ng-model="person.title"></td>
  <td style="text-align: center"><i class="icon-remove" ng-click="removeParticipant()"></i> <span>{{person.errorMessage}}</td>
</tr>

if ($element['empid'] == BAD)
{
  $element['errorMessage'] = 'Invalid Id';  // could bind or show a span/depends.
  $element['error'] = 'error';  // set to whatever class you want to use.
}

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

Hearing the reflow event within the browser

In an attempt to track browser reflow events, I am exploring ways to identify the most resource-intensive parts of my code. Reflow happens when something needs to be redrawn on the screen, such as when a new element is added to the DOM. Is it possible to ...

Objects hidden in the darkness, utilizing the traditional MeshPhongMaterial

Currently struggling with my shadows issue. Here is a snapshot of the problem I am encountering: https://i.sstatic.net/odnuE.png https://i.sstatic.net/RZitM.png The presence of those lines puzzles me, as the scene should be evenly illuminated! The shado ...

Tips for informing flowtype of expanding a partial options object to ensure it is fully developed by a specific stage

Most of you are probably familiar with a very simple use case from your projects. Imagine you have a utility class or function that looks like this: type Options = { foo?: string }; class Something { static get defaultOptions(): Options { ...

Tips for avoiding node js from exposing .git directories and its contents

I understand that by using a .htaccess file, I can restrict files from being served under .git. However, I am unsure of how to achieve the same functionality when using a node.js server. I typically use forever to start and stop the servers. Below is the ...

Utilizing Vue.js to incorporate the isActive property to the class name within a v-for loop, along with implementing dynamic class names

I am currently using a loop to iterate through some data in my store.js file, and everything is functioning as expected. However, I would like to add a condition to check if the Vue instance is active before applying a specific class to the polygon element ...

Pending activation of the Timer Fired event

Below is some code I have for implementing swipe gesture functionality: this.topSlide = this.elementRef.nativeElement.querySelector('.product_rate_slide'); if (this.topSlide) { this.topSlide.addEventListener('touchstart', this.hand ...

Generating fresh line with knockout effect

Currently in the process of developing a Single Page Application (SPA). Utilizing knockout and observable array to iterate through a json array. Encountering an issue where there are br tags present within the text, but when using data-bind="text: myVar" ...

Transmit information to a designated location in Vue

I have a task where I need to showcase data from a dynamically updating list in my project. This particular list gets updated within a function, and what I aim for is to only display this data on the webpage after the said function has been executed succes ...

Iterate over the elements within the AJAX response

One of my buttons triggers an ajax call to a php file, which then sends back some html data as a response. $.ajax({ type: 'POST', data: { cherry: cherry, chocolatechip: choc ...

How to switch between classes for elements and return to the original one when none of the elements is chosen

Hello there, I need some assistance. Here's the scenario: I am working with a grid of six items. My goal is to have the first item in the grid become active by default. When the user hovers over any of the remaining five items, I want the active clas ...

Integrate a feature in React-native MapView that dynamically swaps out markers with different images

Thanks to the insights shared in this informative article, I successfully integrated observations into my MapView. mapMarkers = () => { return this.state.items.map((item) => <Marker key={item.id} coordinate={{ latitude: item.g ...

Issue with Angular 1.5.x - Directive not triggering

Currently, I am honing my skills in working with directives and have been struggling with this particular issue for quite some time. The snippet of code that is causing me trouble is as follows - Module angular.module('wbHeader', []); Contro ...

Using template expressions to access properties that contain spaces

Here is the code structure that I am working with: "name": { "age": [ { "data": { "how old": "23" } }, One of my JSON keys has a space in it. How can I access this pr ...

Utilizing HTML5 to automatically refresh the page upon a change in geolocation

I have a web application built with AngularJS. One of the functionalities is activated only when the user is in close proximity to specific locations. To make this work, I can constantly refresh the page every 5 seconds and carry out calculations to dete ...

What sets apart "import { pick } from 'lodash';" from "import pick from 'lodash/pick';"?

Can you explain the difference between import { pick } from 'lodash'; and import pick from 'lodash/pick'; (Keep in mind that it's 'lodash/pick' in the second one, not just 'lodash'.) How do they each impact ...

Tips for retrieving the hyperlink within an HTML anchor tag using JavaScript

Here's some HTML with JavaScript: document.addEventListener('mousedown', function(event) { let elem = event.target; let jsonObject = { Key: 'mousedown', Value: event.button }; if (event.button == 2) { console ...

Decipher intricate JSON with JavaScript

After retrieving a JSON object from Mongo DB, I have this data structure. **JSON** { "_id" : ObjectId("5265347d144bed4968a9629c"), "name" : "ttt", "features" : { "t" : { "visual_feature" : "t", "type_feature" : ...

"Missing the import of material-ui search icons is causing a functionality issue

import React from 'react' import {Search} from "@material-ui/icons/Search" const App = () => { return ( <div> <Search/> </div> ) } export default App The exported component 'Search' (impor ...

Interoperability between C's tiny-aes-c library and Javascript's CryptoJS

Utilizing the implementation from tiny-aes-c, take a look at this C code snippet: int main(int argc, char const *argv[]) { uint8_t key[6] = { 's','e','c','r','e','t' }; uint8_t iv[16] = ...

Verify whether the elements of the first array are present in the second array, and display the results

Verify whether the second array includes the elements from the first array and display them, otherwise show the elements from the second array that are not in the first one. var contacts = [{name:'muzz',no:1},{name:'muzamil',no:2},{nam ...