【问题标题】:Why does routing in my app doesn't work?为什么我的应用程序中的路由不起作用?
【发布时间】:2016-08-21 17:47:28
【问题描述】:

我使用 angularJS 并想将路由添加到我的应用程序。但我有错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module countriesModule due to:
Error: [$injector:nomod] Module 'countriesModule' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

这是我的文件 app.js:

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

countryApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('data/', {
        templateUrl : 'partials/countries.html',
        controller : 'CountriesListCtrl'
    }).when('data/:countryUrl', {
        templateUrl : 'partials/country-details.html',
        controller : 'CountryDetailsCtrl'
    }).otherwise({
        redirectTo : '/countries'
    });
}]);

还有文件controllers.js:

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

countriesDataModule.controller('CountriesListCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('data/countries.json').success(function (data) {
        $scope.countries = data;
        $scope.selectedProp = 'countryId';
    });
}]);

countriesDataModule.controller('CountryDetailsCtrl', ['scope', '$routeParams', function ($scope, $routeParams) {
    $scope.countyUrl = $routeParams.countryUrl;
}]);

Index.html 文件:

<!DOCTYPE html>
<html lang="en" ng-app="countriesModule">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <script src="bower_components/jquery-2.2.3.min/jquery-2.2.3.min.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <title>Document</title>
</head>
<body>
<div class="container">
    <header>
        <h1>Сountry for travel</h1>
    </header>
    <main>
        <div ng-view></div>
    </main>
</div>
</body>
</html>

我的项目结构:

我访问了其他页面并看到了一些提示:

AngularJS 1.2 $injector:modulerr

Angular JS Uncaught Error: [$injector:modulerr]

https://docs.angularjs.org/error/$injector/modulerr?p0=countriesModule&p1=%E2%80%A62Flocalhost:8080%2Fbower_components%2Fangular%2Fangular.min.js:21:19

但不幸的是它没有帮助我

【问题讨论】:

  • 我只看到了对countriesDataModule的引用
  • 您引用国家模块的地方,代码示例中没有显示
  • 错误:模块“countriesModule”不可用!但是我在我的应用程序中使用了另一个模块国家数据模块。为什么会出现这个错误?
  • 请发布您的 index.html
  • 看上面,我已经添加了index.html

标签: javascript angularjs routes route-provider angularjs-injector


【解决方案1】:

在您共享的代码中没有引用任何名为 countriesModule 的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多