TypeError:无法读取未定义的属性“index”

时间:2014-01-12 05:53:32

标签: javascript angularjs

  

TypeError:无法读取未定义

的属性'index'
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];
}]);

为什么我的索引未定义。我的网址似乎很好,http://localhost:63342/Angular-CRUD/#/edit/2似乎id正在从视图中正确传递,但为什么我getting index在我的控制器中未定义。

1 个答案:

答案 0 :(得分:2)

您尚未传递$routeParams依赖

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