Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  155] [ 4]  / answers: 1 / hits: 15278  / 10 Years ago, sat, february 14, 2015, 12:00:00

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>

More From » angularjs

 Answers
95

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;
}]);

[#67827] Thursday, February 12, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frankiebobbyc

Total Points: 18
Total Questions: 85
Total Answers: 104

Location: Norway
Member since Wed, Jul 7, 2021
3 Years ago
;