Retrieve the $first property from ng-repeat within the controller

I created a table with two input boxes, and by utilizing ng-repeat I have looped through the values:

<tr ng-repeat = "row in allRows" >
<td>

<input type="text" ng-model="row.name" ng-readonly="toogleRN && $first"/>
</td>
<td>
<input type="number" ng-model="row.age" ng-readonly="toogleRN && $first"/>
 </td>
 <td>
  <a role="button" class="btn btn-sm btn-default "
                         data-ng-click="toogleIt()" data-ng-show="$first"><span class="glyphicon glyphicon-search"> Edit</span>
                      </a>
 </td>
 </tr>
 </table>

Here is my controller code:

var app =  angular.module("TableDemo",[]);
app.controller('TableController',['$scope',function($scope){
    $scope.toogleRN=true;

    $scope.toogleIt= function()
    {
        $scope.toogleRN = !$scope.toogleRN;
        console.log($scope.toogleRN);
    };

    $scope.allRows=[{name:'Name_1',age:25},
                    {name:'Name_2',age:28},
                    {name:'Name_3',age:17},
                    {name:'Name_4',age:25},
                    {name:'Name_5',age:26}];
}]);

In order to fulfill the requirement of making the first row read-only by default and editable upon clicking the "Edit" button, I wonder how I can transfer the

ng-readonly="toogleRN && $first"
logic to the controller.

Answer №1

Implement a function :

ng-readonly="customFunction(toggleFlag, $first, row.isEditable)"
// on the button 
 ng-click = row.isEditable = true
 // in the JavaScript
 $scope.customFunction = function(toggleFlag, isFirst, isEditable){

 }

Answer №2

All you really require is the index number of the row. The controller already contains the necessary data.

Therefore, you can achieve it like this:

ng-readonly="toggleRowNumber && $index == 0"

Answer №3

Simply include a new function

ng-disabled="isChecked($index);"

$scope.isChecked = function($index){
    return (!$index && $scope.activateCD)
}

Answer №4

Here's the solution you were looking for:

CSS

<div class="container">
    <ul ng-repeat = "item in itemList" >
        <li>
            <p>{{ item.name }}</p>
            <p>{{ item.description }}</p>
        </li>
    </ul>
</div>

JavaScript

const itemList = [
  { name: 'Item 1', description: 'Lorem ipsum dolor sit amet' },
  { name: 'Item 2', description: 'Consectetur adipiscing elit' },
  { name: 'Item 3', description: 'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua' }
];

$scope.itemList = itemList;

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

I'm encountering issues with adding a div element using Jquery

I've been attempting to insert a div using jQuery, following the code example below. Unfortunately, it's not working as expected. jQuery: $('<div />', { $('<img />', { "src": "/Pages/Images/calendar.png").add ...

Implementing ReactJS render state with loops

Having trouble implementing a responsive upvote button. I use an ajax call named 'users' to get an array of user data, and then loop through it to find if any post IDs match the ones in the 'upvoted' array. When a match is found, it mea ...

There seems to be a discrepancy with the IP to hex conversion equation in my JavaScript code. The result I'm getting doesn't align with what other

How to check if an item is a valid IP address (e.g. 254.253.242.222)? `var h0 = Math.pow(256,0);` `var h1 = Math.pow(256,1);` `var h2 = Math.pow(256,2);` var h3 = Math.pow(256,3); var splitup = item.split('.'); var iHex = ...

Tips for utilizing the .clone() method within a loop or for each individual element of an array

Due to certain requirements, I find myself in a situation where I need to customize the invoice template within the software I use. The invoices are generated in HTML format, prompting me to consider utilizing Stylish and Grease Monkey for this task since ...

Troubleshooting: Error message when using getElementById in JavaScript

Issue: While creating a simple demo site to experiment with JavaScript, I encountered a "TypeError: document.getElementById(...) is null" when attempting to change the display value of an element to block upon button click. Despite trying common solution ...

Developing a Meteor application with the powerful combination of Vue.js and Typescript

I'm struggling to implement Typescript in a Meteor project with Vue. Here are the commands I used to create a project from scratch: Commands meteor create --vue gift-list-app meteor add typescript meteor npm install --save-dev @types/meteor meteor a ...

Angular 2 - Error: Unable to access the property 'createMessage' as it is undefined

I want to import a JavaScript file into my Angular 2 project without having to rewrite it in Typescript. However, when everything is connected correctly, I encounter the following error... "EXCEPTION: TypeError: Cannot read property 'createMessage&ap ...

Discovering all instances of a particular name in JSON using incremented values: a guide

I am looking to automatically detect every occurrence of a specific name in my JSON data. { "servergenre": "Classic Rock", "servergenre2": "pop", "servergenre3": "rock", "servergenre4": "jazz", "servergenre5": "80s", "serverurl": "http://www.n ...

Combining both AJAX variables and HTML classes

Having trouble combining two AJAX variables with JQuery and PHP to add to a MySQL database... Here is the code snippet: $.ajax({ url: DIR+"some.php", method: "POST", dataType: "JSON", data: { mobile: mobile.val(), d ...

Find the furthest distance from the average in a set of data points and identify the corresponding data point

Imagine having an array of data structured like this: var data = [{name: "craig", value: 10}, {name: "oliver", value: 15}] My goal is to create a function that can accept parameters like: function findExtremeValue(windowSize, pointsToTake, data, valueAc ...

Tips for successfully importing mock data for testing without running into reference problems when reusing the same mocked object or array multiple times

Trying to run tests for Angular has presented a challenge. I have intricate objects, nested within other objects, stored in a separate file along with my mocked data. Each mocked object is linked to an export in the data file specifically created for mock ...

What methods can be employed to maintain consistency between front-end and back-end objects in various programming languages?

Our team is currently developing a restful system that utilizes AngularJS on the front end and Spring on the back end. One of our main concerns is ensuring consistency between the objects exchanged across both sides over time, regardless of real-time data ...

What causes the undefined error when a promise rejection is encountered?

I am currently working on a functionality involving checkboxes within a list. The goal is to display an alert message congratulating the user once they have selected 5 checkboxes. I am implementing validation using promises, however, I am encountering an ...

"What is the best way to use jQuery to create a toggle effect where one button controls the state

I'm looking to improve the efficiency of my code, but I'm not sure where to start in terms of making it more concise and organized. Any suggestions on how to streamline this code and keep it neat would be greatly appreciated. $(document).ready(fu ...

The script from '*' is being denied execution because its MIME type ('application/json') is not executable, and a strict MIME type check is in place

Here is the code I used to retrieve data from the confluence rest api: <script type="text/javascript" src="Scripts/jquery.min.js"></script> <script> $.ajax({ type: "GET", url: "https://blog.xxxxx.com/rest/api/content? ...

Verify the password by checking each character as you type

Currently, I am trying to implement a real-time password matching feature. Is there anyone who can provide me with the code that verifies whether the entered passwords match character by character as they are being typed, and also checks the length upon ...

Learn the process of retrieving a file from server.js and showcasing it on an HTML page

I am currently developing a YouTube video downloader using express, HTML, and JavaScript, with plans to transition to Vue.js in the future. Here is a snippet of my code: Index.html <!DOCTYPE html> <html lang="en"> <head> & ...

Is my Bootstrap malfunctioning? The grid system seems to be malfunctioning

My very first question here is quite simple. I have a script that creates rows with dynamic content by appending 4 boxes (columns) in one row and then appending the row to the container. After completing this process, it appends the container to the main c ...

I'm experiencing difficulty getting my code to function properly when adding or removing classes with Bootstrap Glyphicons

Recently, I decided to play around with bootstrap's glyphicons. By utilizing Jquery's addClass and removeClass functions, I tried to smoothly transition from one glyphicon to another. Here is the code snippet: var glyphOn = true; //The glyphO ...

Changing the URI in accordance with the previous URI

I am encountering an issue in my React application where multiple updates of the URI are being made within the same event by adding query parameters using the router.push function from various locations in the code. However, some updates are getting lost b ...