Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $routeParams undefined

I am getting undefined for $routeParams. Here is my code:

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'
            });
    }]);
// add a controller to it
ngAddressBook.controller('ngAddressListController', ['$scope', function ($scope)
    {
        $scope.contacts = [
            {id:1,first_name:'Jane', last_name:'Doe','Phone':'123-456789','Email':'[email protected]'},
            {id:2,first_name:'Jhon', last_name:'Doe','Phone':'123-456789','Email':'[email protected]'}
        ];
        $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>
like image 826
Volatil3 Avatar asked Feb 14 '15 07:02

Volatil3


1 Answers

There is a problem in controller parameter injection. You have not added $routeParams in your array injection list.

ngAddressBook.controller('ngAddressDetailsController', ['$scope','$routeParams', function ($scope,$routeParams)
{
    alert($routeParams);
    $scope.id = $routeParams.id;
}]);
like image 85
Alborz Avatar answered Oct 10 '22 07:10

Alborz