首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复AngularJS中未定义的$route.current

如何修复AngularJS中未定义的$route.current
EN

Stack Overflow用户
提问于 2018-06-03 15:32:00
回答 1查看 328关注 0票数 0

我是AngularJS的新手,目前我得到了一个"TypeError:$route.current is undefined“的提示。请参考下面的代码:

代码语言:javascript
复制
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;
});
代码语言:javascript
复制
<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>

下面是当用户单击"Topic details“时将显示angularParam.html的angularParam.html

代码语言:javascript
复制
<!-- angularParam.html -->

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

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

有人能帮我这个忙吗?提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-04 05:23:04

所以有两件事:

  1. 通过将相同的控制器应用于表元素,它不在路由上,因此没有$route。因此可以从此处删除ng-controller
  2. 您需要使用$route.current.$$route.mytext访问mytext

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50664209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档