【问题标题】:TypeError: Cannot read property 'mytext' of undefined in angularjsTypeError:无法读取angularjs中未定义的属性'mytext'
【发布时间】:2017-07-18 16:27:23
【问题描述】:

我在$routeProvider 中创建了一个自定义键值对,当我尝试在我的控制器中访问此键时,它不起作用并显示为未定义。下面是我的代码

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body ng-controller="AngularCtrl">

<h2>Routes with params</h2>
    <table class="table table-striped">
        <thead>
            <th>
                <td>#</td>
                <td>Topic</td>
                <td>Desc</td>
            </th>
        </thead>
            <tr>
                <td>1</td>
                <td>Controller</td>
                <td><a href="#Angular/1">Topic Details</a></td>
            </tr>
            <tr>
                <td>2</td>
                <td>Models</td>
                <td><a href="#Angular/2">Topic Details</a></td>
            </tr>
            <tr>
                <td>3</td>
                <td>Views</td>
                <td><a href="#Angular/3">Topic Details</a></td>
            </tr>
    </table>

    <div ng-view></div>
</body>
    <script type="text/javascript">
        var app = angular.module("myApp", ["ngRoute"]);

        app.config(function($routeProvider){
            $routeProvider
            .when('/Angular/:topicId', {
                mytext: "This is Angular",
                templateUrl: 'Angular2.html',
                controller: 'AngularCtrl'
            });
        });

        app.controller('AngularCtrl', function($scope, $routeParams, $route){
            $scope.tutorialid = $routeParams.topicId;
            $scope.text = $route.current.mytext;
        });
    </script>
</html>

而我的 Angular2.html 是

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

为什么当我尝试访问它时,控制器中的mytext 显示为未定义?

【问题讨论】:

    标签: javascript angularjs route-provider


    【解决方案1】:

    你可以使用when的resolve函数:

    app.config(function($routeProvider){
            $routeProvider
            .when('/Angular/:topicId', {
                templateUrl: 'Angular2.html',
                controller: 'AngularCtrl',
                resolve: {
                    mytext: function($route){$route.current.params.mytext= 'This is Angular'}
                }
            });
        });
    
        app.controller('AngularCtrl', function($scope, $routeParams, $route){
            $scope.tutorialid = $routeParams.topicId;
            $scope.text = $routeParams.mytext;
        });
    

    【讨论】:

    • L 谢谢它的工作。但是有没有其他方法可以实现这一点。
    猜你喜欢
    • 2015-08-26
    • 2016-09-12
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2016-09-05
    • 2014-12-24
    相关资源
    最近更新 更多