$ injector:modulerr无法实例化模块

时间:2016-04-14 18:32:43

标签: javascript angularjs dependency-injection

我是AngularJS的新手,我遇到了这个例外

  

angular.js:68未捕获错误:[$ injector:modulerr]失败   实例化模块MovieRama由于:错误:[$ injector:modulerr]   由于以下原因无法实例化模块MovieRama.services:错误:   [$ injector:nomod]模块' MovieRama.services'不可用!您   要么错误拼写模块名称,要么忘记加载它。如果注册   模块确保您将依赖项指定为第二个   参数。

我的代码是:

var app = angular.module('MovieRama.services', []);
app.factory('rtAPIservice', function($http) {

    var rtAPI = {};

    rtAPI.getMovies = function() {
        return $http({
            method: 'JSONP',
               url: 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?     page_limit=10&page=1&country=us&apikey=XXXXX&callback=JSON_CALLBACK'
        });
    }

    return rtAPI;
});

app.factory('rtAPIserviceall', function($http) {
    var rtAPIall = {};

    rtAPIall.getMoviesall = function () {
        return $http({
            method: 'JSONP',
               url: 'http://api.rottentomatoes.com/api/public/v1.0/movies.json',{
            params: {
                page_limit: '10',
               page:'1',
               q: $scope.search,
               apikey: 'XXXX',
               callback: 'JSON_CALLBACK'
            }
        }
        })
    }
return rtAPIall;
});

HTML

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="stylesheet" href="/node_modules/angular-material/angular-material.css">
<title>MovieRama</title>
</head>

<body ng-app="MovieRama">

        <table>
            <thead>
                <tr><th colspan="4">MovieRama</th></tr>
            </thead>


            <tbody ng-if= "search != null" ng-controller="moviesController"> 
            <ul ng-repeat="movie in moviesList">

                 <td>

                    <img src={{movie.posters.thumbnail}} />

                    <ul> {{movie.title}}</ul>
                    <ul> Release: {{movie.year}}- Runime: {{movie.runtime}}-Audience rating: {{movie. ratings.audience_score}}</ul>
                    <ul>{{movie.synopsis}}</ul>

               </td>
</tr>

            </tbody>

            <tbody ng-if="search == null">
            <ul ng-repeat="movie in moviesListall" ng-controller="moviesallController">

                <td>
                    <img src={{movie.posters.thumbnail}}/>
                    <ul> {{movie.title}}</ul>
                    <ul> Release: {{movie.year}} - Runtime: {{movie.runtime}} - Audience rating:      {{movie.ratings.audience_score}} </ul>
                    <ul> {{movie.synopsis}} </ul>
                </td>
            </tr>   
            </tbody>

        </table>
        <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/services.js"></script>
        <script src="js/controllers.js"></script>
        <script src="/node_modules/angular/angular.js"></script>
        <script src="/node_modules/angular-aria/angular-aria.js"></script>
        <script src="/node_modules/angular-animate/angular-animate.js"></script>
        <script src="/node_modules/angular-material/angular-material.js"></script>

        </body>
        </html>

app.js

angular.module('MovieRama', [
        'MovieRama.controllers',
        'MovieRama.services'
        ]);

一家工厂的工作正常。有什么问题?

1 个答案:

答案 0 :(得分:0)

将依赖项MovieRama.services添加到引导应用程序的主模块(可能是index.jsapp.js,具体取决于您的文件结构。)

同时删除对<script src="/node_modules/angular/angular.js"></script>的引用,因为您有两个角度文件。将角度文件移到js文件之前的顶部。

另外,为什么将这两种方法作为不同的服务。您可以使用以下两种方法获得One服务:

var app = angular.module('MovieRama.services', []);
app.factory('rtAPIservice', function($http) {

var rtAPI = {};

rtAPI.getMovies = function() {
    return $http({
        method: 'JSONP',
           url: 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?     page_limit=10&page=1&country=us&apikey=XXXXX&callback=JSON_CALLBACK'
    });
}
rtAPI.getMoviesall = function () {
    return $http({
        method: 'JSONP',
           url: 'http://api.rottentomatoes.com/api/public/v1.0/movies.json',{
        params: {
            page_limit: '10',
           page:'1',
           q: $scope.search,
           apikey: 'XXXX',
           callback: 'JSON_CALLBACK'
        }
    }
    })
}
return rtAPI;
});