Explore the scope of each item in ng-repeat

My table has rows that use their respective scope variables for conditional rendering:

<tr ng-repeat="row in data">
  <input type="text" data-ng-show="editMode" data-ng-model="row.smth"/>

The input is only shown when the row's scope variable editMode is true, and it works perfectly fine.

Now I want to add a new row dynamically:

$scope.data.push(my_new_row)

This operation works well. But then I wish to set the new row's scope variable editMode to something, and I'm currently lacking ideas on how to achieve this.

I am aware of an undocumented method involving $$childHead, but I find it inelegant.

Answer №1

To keep track of items, utilize $index in your code. Utilize the index to determine whether an item should be shown or hidden. Create a function that includes the show/hide logic and input the index as a parameter. Here is an example:

<div ng-repeat="row in data track by $index">
    <input type="text" data-ng-show="GetEditMode($index)" ng-model="row.smth[$index]">
</div>

For further information, refer to this link.

Answer №2

To enable edit mode in row data, you have a couple of options.

<input type="text" data-ng-show="row.editMode" data-ng-model="row.smth"/>

Alternatively, you can also try this approach:

<input type="text" data-ng-show="this.editMode" ng-change="toggle(this);" data-ng-model="row.smth"/>

In your Controller, you can access the editMode property like this:

$scope.toggle = function(context){
        console.log(a);
        context.editMode=!context.editMode;
}

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

How to delete the last item of an array in AngularJS using scope

In my Angular controller, I have an array and a method for deleting objects. function($scope, $http){ $scope.arrayOfObjects = []; $scope.remove = function(obj){ var i = $scope.arrayOfObjects.indexOf(obj); if( i > -1 ){ ...

Only consider valid values for input and ignore any zeros

I am working on a form where I need to accept any number, regardless of if it's negative, a float, or a long integer. I have implemented code to not allow null, undefined, or empty values, but I encountered an issue where entering 0 is being read as e ...

Is there a way to incorporate the image that was uploaded from the server onto an HTML page without using the file extension?

$('#userInfoPhoto').html(function () { return '<img src="../uploads/' + thisUserObject.profileimage + '" alt = "userphoto" style="width:250px; height:250px"/>' }); This script is essential for rendering images on th ...

Multer is not recognizing the uploaded file and is returning req.file

This question has definitely been asked multiple times in the past, and I have attempted to implement various solutions without much success. Struggling to upload a file and read its size through Node has left me frustrated. Initially, I tried using the f ...

What is the best way to toggle the visibility of multiple column groups in Ag-Grid community using dynamic

I am seeking to replicate a basic version of the sidebar found in Ag-Grid Enterprise. The goal is to use JavaScript to gather all column groups within a grid and then provide a checkbox for each group to toggle visibility. While I am aware that individual ...

Elastic_Frame | "Error: Unable to process action as property 'toLowerCase' is not defined"

Attempting to create a personal and project portfolio using Vu Khanh Truong's "elastic_grid" jQuery plugin for the actual portfolio. You can access it here. However, encountering some issues on my page: 1) The header for the portfolio section disappe ...

Create an excel spreadsheet using HTML tables with the help of jQuery

Upon clicking a button, I aim to generate an excel sheet. Essentially, I am trying to achieve what is being discussed here (Kalle H. Väravas answer). If the following links are not working within Mozilla browser, then maybe my code requires ActiveXObject ...

Having trouble getting my app.get calls to work in the new Node Express project

Recently, I kicked off a new project and encountered a perplexing issue - my app.get method simply refuses to be invoked. The site keeps loading indefinitely without any content being displayed. Initially, I copied and pasted the code from another project ...

Utilize Node Package to Dispatch Firebase Push Notifications

I am currently experimenting with a NodeJS module for sending Push Notifications. I have been exploring the 'node-sender' module but have not been able to find an option for sending notifications to a web browser for testing purposes: https://w ...

What is the best way to retrieve the innerHTML content of an anchor tag with Cheerio?

Looking to extract data from an HTML page, a simplified example is provided below. Upon running the code, I anticipate the output to be [ "foo", "baz", "quux", ] but instead encounter an error message stating "TypeError: anch ...

Learn how to efficiently print data from an object in AngularJS without repetition

My objective is to display an object using ng-repeat while taking into account that some keys are duplicated. I would like the duplicate keys to be displayed only once, and for the average of their similar values to be calculated. Here is the data: [ ...

Next.js page transitions causing Google Tag Manager to malfunction with 'react-gtm-module'

I am currently working on a Next.js project and have successfully integrated Google Tag Manager (GTM) using the react-gtm-module package. Interestingly, everything works perfectly when the application is initially loaded in debug mode, regardless of the st ...

Successful AJAX Post request functioning exclusively within the Web Console (Preview)

When a user clicks on an element on the website, I have a simple AJAX request to send the ID of that element. However, the script only works in the Web Console under the Network -> Preview section and this issue occurs across all browsers. Here is the ...

Incorporate XHR into a JavaScript Function

Upon running the code below, a GET message is displayed in the Firebug Console. However, the variable appears as undefined, most likely due to its limited scope within the function. The objective here is to click on a table row, fetch relevant data from t ...

Using Javascript to automatically submit a form when the enter key is pressed

Can anyone help me with a password form issue? I want the enter key to trigger the submit button but it's not working for me. I understand that the password can be viewed in the source code, this is just for practice purposes. <!DOCTYPE html> & ...

Spinning an object smoothly in the opposite direction of the OrbitControls camera's y rotation in three.js

Is there a way to use OrbitControls in three.js to make a planeMesh always remain facing the camera? I've attempted some code but it's not quite working as expected. I want the planeMesh to slowly readjust its orientation only when the camera mov ...

What is the best way to define 'this' context and reference an instance of an Angular 6 component?

I have successfully created a demo featuring an Earth globe using D3 and JS. Now, I am exploring the process of transforming it into an Angular 6 component. Below is the full demo without Angular: import * as d3 from 'd3v4'; import { Component ...

Unable to link to '' because it is not recognized as a valid attribute of '' in Angular 2

I encountered an exception while working on my Angular 2 project and I'm struggling to figure out the cause. Below is the snippet of my code: ts: import {Component} from "@angular/core"; import {GridOptions} from "ag-grid"; import {RedComponentComp ...

When a form contains a ViewChild element, changes to the ViewChild element do not automatically mark the

Let's set the stage: MainComponent.html <form #someForm > <input type="text" name="title" [(ngModel)]="mainVar" /> <child-component /> <input type="submit" [disabled]="someForm.form.pristine" /> </form> ChildComp ...

Having trouble with your Javascript Ajax call?

I've been attempting to make a POST request using Ajax, but I keep encountering an error with status code 0. Strangely, all the request parameters seem to be functioning correctly in the Advanced REST Client. Here's My Code Snippet: <button& ...