javascript AngularJS $routeParams 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28513457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:05:49  来源:igfitidea点击:

AngularJS $routeParams undefined

javascriptangularjs

提问by Volatil3

I am getting undefinedfor $routeParams. Here is my code:

我得到undefined了 $routeParams。这是我的代码:

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

索引.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>

回答by Alborz

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

控制器参数注入有问题。您尚未添加$routeParams到阵列注入列表中。

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

回答by schasoli

As I found this on top on google: apart from the missing injection - asign the object $routeParamsand not the Key $routeParams.id

正如我在谷歌上发现的那样:除了丢失的注入 - 分配对象$routeParams而不是密钥$routeParams.id

I found the explination here (from Sander Elias)

我在这里找到了解释(来自 Sander Elias)

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