组件$ injector中出错

时间:2014-02-13 20:34:38

标签: javascript angularjs angularjs-routing

我正在我的app.js

中加载我的观点
// create the module and name it testApp
var testApp = angular.module('testApp', ['ngRoute']);

// configure our routes
testApp.config(function($routeProvider) {
    $routeProvider

    // route for the contact page
    .when('/contact', {
        templateUrl : 'js/ng-app/views/contact.html',
        controller  : 'contactController'
    });

    .when('/search', {
        templateUrl : 'js/ng-app/views/search.html',
        controller  : 'searchController'
    });

});

我的搜索部分看起来像这样:

                                 

我的searchController.js

testApp.controller("searchController", function($scope, $http){

    $scope.apiKey = "97f25560b91ea3ad6c84e6f8c7677e4d";
    $scope.results = [];
    $scope.filterText = null;
    $scope.init = function() {
        //API requires a start date
        var today = new Date();
        //Create the date string and ensure leading zeros if required
        var apiDate = today.getFullYear() + ("0" + (today.getMonth() + 1)).slice(-2) + "" + ("0" + today.getDate()).slice(-2);
        $http.jsonp('http://api.trakt.tv/calendar/premieres.json/' + $scope.apiKey + '/' + apiDate + '/' + 30 + '/?callback=JSON_CALLBACK').success(function(data) {
            //As we are getting our data from an external source, we need to format the data so we can use it to our desired affect
            //For each day get all the episodes
            angular.forEach(data, function(value, index){
                //The API stores the full date separately from each episode. Save it so we can use it later
                var date = value.date;
                //For each episodes add it to the results array
                angular.forEach(value.episodes, function(tvshow, index){
                    //Create a date string from the timestamp so we can filter on it based on user text input
                    tvshow.date = date; //Attach the full date to each episode
                    $scope.results.push(tvshow);
                });
            });
        }).error(function(error) {

        });
    };
});

但是我得到了:

Module Error
error in component $injector
Failed to instantiate module testApp due to:
Error: [$injector:nomod] http://errors.angularjs.org/1.2.8/$injector/nomod?p0=digi...
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:6:449
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:20:260
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:21:262
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:29:175
    at Array.forEach (native)
    at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:7:274)
    at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:29:115)
    at $b (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:32:232)
    at Zb.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js:17:431

我认为这是因为我使用ngRoute。但是,如何解决此问题而不删除ngRoute

感谢您的回复?

1 个答案:

答案 0 :(得分:1)

ngRoute模块(或角度路线)不再与angular.js捆绑在一起,需要单独包含。你记得这么做过吗?

来自docs

  

$ route用于将URL深层链接到控制器和视图(HTML   谐音)。它监视$ location.url()并尝试将路径映射到   现有路线定义。

     

需要安装ngRoute模块。

here是文档页面,其中包含有关如何获取angular-route.js的信息。

ngRoute从版本1.2.0角度移动到自己的模块。来自changelog

  

由于5599b55b,使用$ route的应用程序现在需要加载一个   angular-route.js文件并定义对ngRoute模块的依赖。