繁体   English   中英

AngularJS错误:[$ injector:unpr]未知提供程序

[英]AngularJS Error: [$injector:unpr] Unknown provider

定义URL路由后,出现以下错误-出于学习目的,从AngularJS书中的sportsStore应用程序开始。

  1. 错误:[$ injector:unpr]未知提供程序:$ templateRequestProvider <-$ templateRequest <-$ route <-ngViewDirective
  2. 错误:[$ injector:cdep]找到循环依赖项:ngViewDirective

我已经阅读了与此类错误相关的所有文章,并检查了angular.js和angular-route.js的版本是否相同(最后一个稳定版本)。 我还阅读了有关AngularJS API的文档,并确保此处描述的原因不是这种情况。

我不知道下一步该怎么做,因为我无法理解图中显示的浏览器开发人员工具中的错误。 请指出正确的方向。 在此处输入图片说明

这是app.html,其中定义了路由以显示特定视图:

 <!DOCTYPE html> <html ng-app="sportsStore"> <head> <title>SportsStore</title> <script src="angular.js"></script> <link href="bootstrap.css" rel="stylesheet" /> <link href="bootstrap-theme.css" rel="stylesheet" /> <script> angular.module("sportsStore", ["customFilters", "cart", "ngRoute"]) .config(function($routeProvider) { $routeProvider.when("/checkout", { templateUrl: "/views/checkoutSummary.html" }); $routeProvider.when("/products", { templateUrl: "/views/productList.html" }); $routeProvider.otherwise({ templateUrl: "/views/productList.html" }); }); </script> <script src="controllers/sportsStore.js"></script> <script src="filters/customFilters.js"></script> <script src="controllers/productListControllers.js"></script> <script src="components/cart/cart.js"></script> <script src="ngmodules/angular-route.js"></script> </head> <body ng-controller="sportsStoreCtrl"> <div class="navbar navbar-inverse"> <a class="navbar-brand" href="#">SPORTS STORE</a> <cart-summary /> </div> <div class="alert alert-danger" ng-show="data.error"> Error ({{data.error.status}}). The product data was not loaded. <a href="/app.html" class="alert-link">Click here to try again</a> </div> <ng-view /> </body> </html> 

在不更改代码的情况下,我还有另一个错误。 这太奇怪了: 在此处输入图片说明

您在加载angular-route.js文件之前编写routes 因此,您需要将routes<script></script>之间移动到最后。

这样可以解决您的问题。

移动

 <script> angular.module("sportsStore", ["customFilters", "cart", "ngRoute"]) .config(function($routeProvider) { $routeProvider.when("/checkout", { templateUrl: "/views/checkoutSummary.html" }); $routeProvider.when("/products", { templateUrl: "/views/productList.html" }); $routeProvider.otherwise({ templateUrl: "/views/productList.html" }); }); </script> 

到“ head”的末尾,导致仍然未加载angular和ngRoute。 最好将脚本放在“ body”的底部

@uamanager向我展示了解决方案->在templateUrl中的视图之前删除'/'

<script>
    angular.module("sportsStore", ["customFilters", "cart", "ngRoute"])
    .config(function ($routeProvider) {

        $routeProvider.when("/checkout", {
            templateUrl: "views/checkoutSummary.html"
        });

        $routeProvider.when("/products", {
            templateUrl: "views/productList.html"
        });

        $routeProvider.otherwise({
            templateUrl: "views/productList.html"
        });
    });
</script>

更改路由提供者的语法,只需一个$routeProvider

$routeProvider
    .when('/Book/:bookId', {
        templateUrl: 'book.html',
        controller: 'BookController',    
   })
   .when('/Book/:bookId/ch/:chapterId', {
       templateUrl: 'chapter.html',
       controller: 'ChapterController'
  });

看看这个codepen https://codepen.io/CarterTsai/pen/tfjqu

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM