How to pass the ng-repeat $index value as a parameter in AngularJS

Within the code snippet provided, there is a shell page index.html and a partial view currently utilized by two different controllers. The static data in AppController is connected to the list.html partial view and displayed as a table. In the JavaScript portion, I inserted a console.log statement to monitor when the controllers are invoked. Upon app initialization, AppController executes. When invoking #/new, NewController triggers. However, upon clicking the edit button next to each row, EditController fails to activate. EditController should utilize the /partials/edit.html view while pre-populating the fields with information from the clicked row. In this case, crew[0] represents Picard, whose details should auto-fill when the icon is clicked on. Despite not encountering any errors, EditController's view isn't injected at the expected time.

JS

angular.module('enterprise', ['ngRoute'])
    .config(function ($routeProvider) {
        $routeProvider
            .when("/", { templateUrl: "/partials/list.html" })
            .when("/new", { templateUrl: "/partials/edit.html", controller: "NewController" })
            .when("/edit:id", { templateUrl: "/partials/edit.html", controller: "EditController" });
    })
// Repetitive section used in partial views ng-repeat
function AppController($scope){
    $scope.crew = [
        { name: 'Picard', description: 'captain' },
        { name: 'Riker', description: 'number 1' },
        { name: 'Word', description: 'Security' }
    ];
        console.log('app controller hit');        
}
function NewController($scope, $location) {
    $scope.person = { name: "", description: "" };
    $scope.save = function () {
        $scope.crew.push($scope.person);
        $location.path("/");
    }
    console.log('new controller hit');
}
function EditController($scope, $location,$routeParams) {
    $scope.person = $scope.crew[$routeParams.id];
    console.log('edit controller hit');
}

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Angular JS Routing</title>
    <link rel="stylesheet"
          href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"/>
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css"
          rel="stylesheet">
</head>
<body>
<div ng-app="enterprise" ng-controller="AppController">
    <a href="#"><h2>Enterprise Crew</h2></a>
    <ng-view></ng-view>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>

list.html

<table class="table table-striped" style="width: 250px">
    <thead>
    <tr>
        <td>Name</td>
        <td>Description</td>
        <td> <a href="#/new"><i class="glyphicon glyphicon-plus-sign"></i></a>

        </td>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="person in crew">
        <td>{{person.name}}</td>
        <td>{{person.description}}</td>
        <td><a href="#/edit/{{$index}}"><i class="glyphicon glyphicon-edit"></i></a></td>
     During the ng-repeat loop, an attempt is made to navigate to the edit.hml partial view and fill the text boxes in `edit.html` with data from the selected row
    </tr>
    </tbody>

</table>

edit.html

<form action="">
    <input ng-model="person.name"/ placeholder="Enter the person name">
    <input ng-model="person.description"/ placeholder="Enter the description">
    <button ng-click="save()" class="btn-primary">Save</button>
</form>

No specific errors have been identified, yet EditController remains inactive. Any insights into why this might be happening would be greatly appreciated. Thank you.

Answer №1

Make sure to use "/edit/:id" instead of "/edit:id" within your

.when("/edit:id", { templateUrl: "/partials/edit.html", controller: "EditController" });

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

Exploring the capabilities of a Vue.js component

I am currently facing some challenges while trying to test a Vue.js component. My main issue lies in setting a property for the component and verifying that it has been set correctly. For context, the module has been loaded with exports and the JavaScrip ...

Exploring AngularJS capabilities: iterating over a collection of objects generated by a method

My latest project involves retrieving data from an API using a controller. Here's the code snippet: .controller('MyDataController', ['$http', function($http) { pdc = this; pdc.projects = [] pdc.getData = function(val ...

What is the material-ui component that mirrors the functionality of <input type="color"><datalist>?

I'm just starting out with javascript, react, and Material-UI, so my question (along with the code sample) might show my lack of experience. Within a Material-UI TableCell (not a form), I have the following code: <input type="color" name ...

Update all the key values in the JSON file to be empty strings

Looking for a solution to modify the values in a list of flat JSON key-value pairs by replacing them with empty strings. Below is an example of the JSON data: { "wl.label.accountPin": "Account PIN", "wl.label.logon": ...

What precautions should I take to safeguard my HTML/CSS/JS projects when sharing a link with my employer?

Picture this scenario: you need to share a link for your work, but you want to protect it from being easily copied or developed further by someone else. However, since it's a document, any browser can still view it. Can anyone recommend a tool that wi ...

How can I use AJAX to send a dynamically generated PDF file?

I utilized jsPDF to create a PDF object. My goal is to send an email with a PDF attachment using AJAX, but I am facing issues in sending the file correctly. I attempted to convert it into a Blob object for transmission and later decode it to base64 in PHP ...

Is a Toolbar plugin or custom Toolbar options the better choice for your project?

Could anyone recommend a Jquery plugin for adding a ToolBar option to my web application? I've searched and researched for the past 48 hours but haven't found a reliable one yet. If the recommended toolbar resembles the image below, that would b ...

The text content is not in alignment with the server-rendered HTML for translation purposes with i18n

I have successfully implemented i18n in my Next.js project. The folder structure for my locales is as follows: public/locales/en/translation.json and public/locales/fr/translation.json The error I am encountering is: Uncaught Error: Text content does n ...

Replace the value of Undefined with an empty string

I have been utilizing exif.js to extract metadata from the images I upload on a content management system (CMS). However, sometimes when an image does not contain any metadata, certain values may show up as "undefined". My goal is to replace these "undefin ...

Issues with the functionality of straightforward AngularJS routing

I cannot figure out why the add screen is not routing correctly. Can anyone help me troubleshoot? Here is my app.js var moviesApp = angular.module('moviesApp', ['ngRoute']); moviesApp.config(function($routeProvider) { $routeProvid ...

Passport session is lost following oauth callback

Developing a hybrid app using Ionic, Angular, nodejs, etc. After the user logs in with their email and password, they want to include 3rd party authentication for their account. Once the user is serialized into session, we verify their authorization sta ...

What are the best ways to personalize my code using Angular Devextreme?

Our development team is currently utilizing Angular for the front-end framework and ASP.net for the back-end framework. They are also incorporating Devextreme and Devexpress as libraries, or similar tools. However, there seems to be difficulty in implement ...

React and D3 Force Layout: uncharted territories for new links' positions

After carefully following the general update pattern for new React Props, I've noticed that D3 efficiently handles data calculation and rendering when receiving new props. This prevents React from having to render every tick. D3 functions seamlessly ...

What is the best way to list all registered states in ui-router?

Is there a method available to display a list of all the states that are registered when utilizing ui-router? ...

Ways to Identify When the Back Button is Clicked

Currently, I am developing an HTML website that contains 4 divs on the index.html page. Initially, when clicking on a div, it would expand while the other sections reduced in width. However, the client requested that the URL should change when clicking o ...

Every page on Nextjs displaying identical content across all routes

I recently deployed a Next.js app using docker on AWS infrastructure. While the index page (/) loads correctly, I've noticed that the content of the index is also being loaded for every other route, including api routes, as well as the JavaScript and ...

NgModel in Angular Datapicker does not successfully transmit value

My page features a form that functions as a filter search, with one of the fields being a date field. I have configured the Angular UI datepicker plugin (https://github.com/angular-ui/ui-date) and the calendar pops up when I focus on the date field. Howe ...

Enzyme fails to locate text within a div even though it is present

In my current test scenario, I have a set of data displayed in Material-UI Cards. The data is provided through a JSON array consisting of objects with fields like projectName, crc, dateCreated, createdBy, and lastModified. { projectName: 'P ...

Don't allow users to switch views without saving their changes

We are working with a Backbone.js application that presents various forms to users. Our goal is simple: if a user navigates away from the page without saving the completed form, we need to show a confirmation dialog. When dealing with traditional forms, i ...

Responsive design issues with Bootstrap 3 box layout

I am currently working on creating a bootstrap4 layout that includes three boxes arranged side by side on a wide screen. However, my goal is to ensure that if the screen size decreases, the red and green boxes always stay adjacent to each other while the b ...