0

TypeError: Cannot read property 'index' of undefined

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

crud.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/edit/:index', {
        templateUrl: 'js/views/edit.html',
        controller: 'EditCtrl'
    }).when('/',{
        templateUrl: 'js/views/list.html'
    })
}]);

crud.controller('EditCtrl',['$scope', function(scope,$routeParams){
    scope.name = scope.names[$routeParams.index];
}]);

Why am i getting index undefined. My url seems to be fine, http://localhost:63342/Angular-CRUD/#/edit/2 It seems that id is passing correctly from view, but why am i getting index undefined in my controller.

3
  • On which line does this error occur? Commented Jan 12, 2014 at 5:55
  • scope.name = scope.names[$routeParams.index]; on this line. Commented Jan 12, 2014 at 5:57
  • It's not index that's undefined, it's the object from which you're trying to access index that is undefined, which would be $routeParams (from the expression $routeParams.index). Commented Jan 12, 2014 at 6:09

1 Answer 1

2

You have not passed $routeParams in dependency

                                     //here
crud.controller('EditCtrl',['$scope','$routeParams', function(scope,$routeParams){
    scope.name = scope.names[$routeParams.index];
}]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.