AngularJs:错误:[ng:areq]

时间:2014-06-08 22:42:59

标签: angularjs angularjs-routing

我对AngularJs或多或少是全新的,我有一个问题。我试图将路由添加到我的角度应用程序中,但它并不顺利。

我有一个模板,基本上按此顺序加载js:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.2.3/js/foundation.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular-route.min.js"></script>     
<script src="js/app.js"></script>
<script src="js/controllers/mainCtrl.js"></script> <!-- load our controller -->
<script src="js/services/userService.js"></script> <!-- load our service -->
<script src="js/routes/routes.js"></script> 

似乎装好了,订单似乎很好,但我得到了Error: [ng:areq]。 这似乎意味着我两次调用angular.module()。

视图加载的位置如下:

<body ng-app='blogApp'>
        <header></header>
        <section>
            <div ng-view></div>
        </section>

        <footer></footer>
    </body>

app.js看起来像:

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

mainCtrl看起来像:

blogApp.controller('mainController', function($scope, $http, User){
            $scope.formData = {};
            $scope.login = function(){
              User.login($scope.formData)
                      .success(function(){
                          console.log("logged in");
                      })
                      .error(function(){
                          console.log("Unable to log in!");
                      });
            };
            $scope.register = function(){
                console.log("clicked!");
              User.register($scope.formData)
                      .success(function(){
                          console.log("registered");
                      })
                      .error(function(){
                          console.log("Unable to register!!");
                      });
            };
            $scope.logout = function(){
                User.logout().success( function(){
                                console.log("logged out!");
                                }   
                        )
            }
        });

userService看起来像:

blogApp.factory('User', function($http){
           return {
               login : function(formData){
                   return $http({
                            method: 'POST',
                            url: '/angularBlog/public/users/login',
                            headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                            data: $.param(formData)
                    });                   
               },
               register : function(formData) {
                   return $http({
                            method: 'POST',
                            url: '/angularBlog/public/users/register',
                            headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                            data: $.param(formData)
                    });                        
               },
               logout : function(){
                   return $http({
                            method: 'POST',
                            url: '/angularBlog/public/users/logout',
                            headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }
                    });                    
               }
           } 
        });

和routes.js看起来像:

blogApp.config(function($routeProvider) {
        $routeProvider
            // route for the home page
            .when('/', {
                templateUrl : 'home',
                controller  : 'mainCtrl'
            });
    });

在我添加路由之前,它一切正常,所以我不确定出了什么问题?任何帮助,将不胜感激!如果您需要更多信息,请告诉我,我会告诉您的!

1 个答案:

答案 0 :(得分:2)

你有控制器:

blogApp.controller('mainController

但是你要添加一条带有MainCtrol的路线

       .when('/', {
            templateUrl : 'home',
            controller  : 'mainCtrl'
        });

你能尝试解决这个问题吗?

把:

       .when('/', {
            templateUrl : 'home',
            controller  : 'mainController'
        });