AngularJS $routeParams is not defined

When trying to access $routeParams, I am receiving an undefined value. Check out my code below:

var ngAddressBook = angular.module('ngAddressBook', ['ngRoute']);

ngAddressBook.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'views/list.html',
                controller: 'ngAddressListController'
            }).
            when('/add', {
                templateUrl: 'views/add.html',
                controller: 'ngAddressListController'
            }).
            when('/details/:id', {
                templateUrl: 'views/details.html',
                controller: 'ngAddressDetailsController'
            });
    }]);

ngAddressBook.controller('ngAddressListController', ['$scope', function ($scope)
    {
        $scope.contacts = [
            {id:1,first_name:'Jane', last_name:'Doe','Phone':'123-456789','Email':'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e58f848b80a5809d8488958980cb868a88">[email protected]</a>'},
            {id:2,first_name:'Jhon', last_name:'Doe','Phone':'123-456789','Email':'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="016b696e6f416479606c716d642f626e6c">[email protected]</a>'}
        ];
        $scope.getTotalContacts = function () {
            return $scope.contacts.length;
        };
    }]);

ngAddressBook.controller('ngAddressDetailsController', ['$scope', function ($scope,$routeParams)
{
    alert($routeParams);
    $scope.id = $routeParams.id;
}]);

index.html

<!doctype html>
<html ng-app="ngAddressBook">
<head>
    <title>Ng Addressbook</title>
    <link href="main.css" rel="stylesheet" media="screen" />
</head>
<body>
    <div ng-controller="ngAddressListController">
        <div id="container">
            <h1>Ng Address Book</h1>
            <div id="content" ng-view>
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
    <script src = "angular-route.min.js"></script>
    <script src="main.js"> </script>
</body>
</html>

Answer №1

It seems like there is an issue with the parameter injection in the controller. Make sure to include $routeParams in your array of injected dependencies.

ngContactBook.controller('ngContactDetailsController', ['$scope','$routeParams', function ($scope,$routeParams)
{
    alert($routeParams);
    $scope.id = $routeParams.id;
}]);

Answer №2

While doing a quick search on Google, I came across an interesting tip: Instead of assigning the object $routeParams, make sure to assign the object itself and not the key $routeParams.id.

You can find more details about this explanation here (courtesy of Sander Elias)

ngAddressBook.controller('ngAddressDetailsController', ['$scope','$routeParams', function ($scope,$routeParams)
{
    $scope.id = $routeParams
    alert($routeParams);
}]);

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

Having trouble getting rid of the border-bottom?

I have been attempting to customize the appearance of the React Material UI tabs in order to achieve a design similar to this: https://i.stack.imgur.com/tBS1K.png My efforts involved setting box-shadow for the selected tab and removing the bottom border. ...

In order to comply with JSX syntax rules in Gatsby.js, it is necessary to enclose adjacent elements

I want to apologize beforehand for the code quality. Every time I attempt to insert my HTML code into the Gatsby.js project on the index.js page, I encounter this error: ERROR in ./src/components/section3.js Module build failed (from ./node_modules/gatsb ...

What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt: <v-row> <v-col ...

My angularjs app hosted on Heroku is experiencing difficulty caching S3 images in the browser, despite setting cache-control headers

I am currently facing an issue with enabling browser caching for S3 images. My AngularJS app is deployed on Heroku and utilizes images stored in an S3 bucket. Despite my efforts, the browser does not cache the images as intended. Instead, it repeatedly req ...

Manipulating prop values through dropdown selection

I'm currently working on implementing filtering based on a prop value that changes according to the dropdown selection. Here's my progress so far: template(v-for="field in tableFields") th(:id="field.name") select(@change="filterScope(sc ...

Customizing the appearance of a JavaScript countdown timer's output on an individual basis

I am currently working on customizing the appearance of a JavaScript date counter. My goal is to style the days, hours, minutes, and seconds individually. Ideally, I want each unit to be right-aligned within its own div, with specific left and top absolute ...

The additional values inserted into the form using jQuery are not being recognized or passed to AngularJS

My form has multiple input fields, some of which are pre-populated when the markup is generated. For example, value=#{ session[:lat] } or simply value='111'. While this appears correct when inspecting the page, Angular does not submit this value. ...

Using React-router-dom's Link component can cause styling inconsistencies with material-ui's AppBar Button

Exploring the use of a material-ui Button with react-router-dom's Link is showcased here: import { Link } from 'react-router-dom' import Button from '@material-ui/core/Button'; <Button component={Link} to="/open-collective"> ...

A method for saving information to cookies without encoding it

Is there a way to insert raw data into a cookie without AngularJS encoding it further? I have my own encoding logic and don't want AngularJS to apply additional encoding on the data. Edit: For example, I want to save the following string in a cookie: ...

AngularJS ng-repeat is not displaying the complete set of data within the factory array

I am encountering an issue with ng-repeat in my Angular project. In my application, I have two tables - one displays data and allows users to click on each element to view more information in the second table. The problem arises when using ng-repeat in t ...

Choosing a line object with the mouse in Three.js

How can I select a line object in threeJS? I attempted to use raycaster for this purpose. http://jsfiddle.net/nelsonlbtn/z43hjqm9/43/ Upon running the test, I encountered the following error: three.min.js:598 Uncaught TypeError: d.distanceTo is not a f ...

Adjust the Material UI card to fill the maximum available height

I'm currently working with Material UI Card components. You can find more information here. My goal is to set a maximum height for these cards, ensuring that the text and images are displayed nicely. How should I approach this? Below is a snippet of ...

Filtering data using Angular's ion-radio component from an external database

Having two tables in Azure db - one called team and one called league, where the id in league is associated with a team (e.g. team A belongs to league ID 1), I am attempting to implement ion-radio for users to select their team, filtered by the league. How ...

Steps to stop POST requests sent via AJAX (acquired through Firebug)

Is there any way to protect against users spamming a post request? Consider a scenario where a form is submitted through an Ajax post. I've observed that the post request can be duplicated by simply right clicking on it and choosing "open in a new tab ...

Is it possible to retrieve the index of a chosen v-select option within Vuetify?

Exploring Vuetify for the first time, I'm encountering an issue with obtaining the index of a selected option on the v-select component. My goal is to populate a text field based on the option that is clicked once I have the index. I am fetching an ...

Guide to animating the height of a div using jQuery:

I'm hoping to create a cool animation effect where a certain part of a div expands to 100% width when clicked, and then reverts back to its original height on a second click. I've tried writing some code for this, but it's not working as exp ...

Can you identify any issues with this Javascript code's "If" condition?

In my JavaScript code, I have an "if condition" that looks like this: for (var i in data) { //Gender.push("Gender " + data[i].JenisKelaminID); if (data[i].JenisKelaminID == 1) { Gender.push("Men"); } if (data[i].JenisKelaminID == 2) { Gend ...

Is it better to set the language of Puppeteer's Chromium browser or utilize Apify proxy?

Looking to scrape a website for French results, but the site supports multiple languages. How can I achieve this? Is it best to configure Puppeteer Crawler launch options using args, like so: const pptr = require("puppeteer"); (async () => { const b ...

Transform the v-model value from numerical to textual representation

Currently, I am using the <q-select> component and populating it with options fetched from an API. The issue arises when setting the value as the ID of the object, which is a number while the component expects a string, resulting in an error. <s- ...

Continuously iterate through a PHP page until the session variable reaches a specific value

I am in the process of creating a web-based exam. All questions and answers are stored in a MySQL database. I began by retrieving and displaying one question along with multiple-choice answers. I used a session variable $_SESSION['questionno'] = ...