【问题标题】:AngularJS $route gives error: [$injector:unpr]AngularJS $route 给出错误:[$injector:unpr]
【发布时间】:2014-12-15 11:27:54
【问题描述】:

我刚开始学习 AngularJS,并尝试使用 $route 显示不同的视图,但在我的控制台中出现错误。这是我的代码:

index.html

<!Doctype html>
<html ng-app="approute">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="">
        <title>Search The Angles: {{people.name}}</title>
        <link rel="stylesheet" href="css/main.css">
        <script type="text/javascript" src="script/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="script/angular.min.js"></script>
        <script type="text/javascript" src="script/angular-route.js"></script>
        <script type="text/javascript" src="script/app.js"></script>
    </head>
    <body>
        <input type="text" ng-model="test">{{test | json}}

        <div ng-view></div>

        <a href="#/path2">Html2</a>
    </body>
</html>

而我的脚本如下:

'use strict';

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

approute.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/path2', {
            templateUrl: "partials/index2.html",
            controller: "appCountroller"
        })
        .when('/', {
            templateUrl: "partials/index1.html",
            controller: "appCountroller"
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

approute.controller("appCountroller", function($scope, $http) {
    $http.get('../json/data.json').success(function(response) {
        $scope.people = response;
    });
});

index1index2.html 格式如下:

<div>
    <ul>
        <li ng-repeat="person in people | filter:test">{{person.name}}</li>
    </ul>
</div>

index2.html

<div>
    <ul>
        <li ng-repeat="person in people | filter:test">{{person.name}}-{{person.position}}</li>
    </ul>
</div>

当我尝试在浏览器中打开时,我的控制台上出现以下错误:

错误:[$injector:unpr] http://errors.angularjs.org/1.2.27/$injector/unpr?p0=%24templateRequestProvider%20%3C-%20%24templateRequest%20%3C-%20%24route%20%3C-%20ngViewDirective 在错误(本机)...

参考我的 angular.min.js 文件

我已经检查了所有可能的内容,但没有看到导致错误的原因。

【问题讨论】:

  • 你能为此更新一个 plunker 吗?这应该工作
  • 根据您的错误,您使用的是 Angular 1.2.27。可能会出现错误,因为您使用的是不同版本的 angular.route。你确定吗? btw here 是您的代码的工作示例。

标签: javascript angularjs


【解决方案1】:

似乎无法解决依赖关系。在这种情况下,它看起来像是关于 ng-route 中的 templateRequestProvider。

检查 angular-route 的版本和 angular 的版本不冲突,我想可能是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 2016-10-21
    • 2018-11-19
    • 2016-11-12
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多