【问题标题】:How to fix $route.current is undefined in AngularJS如何修复 $route.current 在 AngularJS 中未定义
【发布时间】:2018-06-03 07:32:00
【问题描述】:

我是 AngularJS 的新手,目前我收到“TypeError: $route.current is undefined”。请看下面的代码:

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

sampleApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/Angular/:topicId', {
        mytext: 'This is angular',
        controller: 'angularControllerParam',
        templateUrl: 'angularParam.html'
    });
}]);    

sampleApp.controller('angularControllerParam', function($scope, $routeParams, $route) {
    $scope.tutorialid = $routeParams.topicId;
    $scope.text = $route.current.mytext;
});
<body ng-app="sampleApp">
        <h1>Accessing parameters from the route</h1>
        <table class="table table-stripped" ng-controller="angularControllerParam">
            <thead>
                <tr>
                    <th> # </th>
                    <th> Angular JS topic </th>
                    <th> Description </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td> 1 </td>
                    <td> 1 </td>
                    <td> Controllers </td>
                    <td> <a href="#!Angular/1"> Topic details </a> </td>
                </tr>
                <tr>
                    <td> 2 </td>
                    <td> 2 </td>
                    <td> Models </td>
                    <td> <a href="#!Angular/2"> Topic details </a> </td>
                </tr>
                <tr>
                    <td> 3 </td>
                    <td> 3 </td>
                    <td> Directives </td>
                    <td> <a href="#!Angular/3"> Topic details </a> </td>
                </tr>
            </tbody>
        </table>
        <div ng-view></div>
    </body>

下面是 angularParam.html,当用户点击“主题详细信息”时,它将显示 angularParam.html

<!-- angularParam.html -->

<h2>Angular</h2>
<div>{{ text }}</div>
<div>{{ tutorialid }}</div>

我想要实现的是访问路由的属性,即在我的 angularParam.html 中显示“This is angular”文本

有人可以帮我解决这个问题吗?提前致谢。

【问题讨论】:

标签: html angularjs angularjs-routing


【解决方案1】:

所以两件事:

  1. 通过将相同的控制器应用于表元素,它不在路由上,因此没有$route。所以ng-controller 可以从那里删除。

  2. 您需要改用$route.current.$$route.mytext 来访问mytext

查看我创建的 plnk:https://plnkr.co/edit/2jmErSMSSK7lA9cZPQ3m?p=preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2019-09-16
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多