使用AngularJS我的js文件有问题:“错误:[ng:areq]”

问题描述 投票:0回答:2

我正在组建AngularJS,当我启动应用程序时,它每次都会给我这个错误。我看了很长时间,我无法找到。我已经检查了文件和模块的名称。有任何想法吗?

错误:http://prntscr.com/nd45d2

文件:app.blade.php

<!DOCTYPE html>
<html lang="en" ng-app="app">
    ...
    <div ng-view> </div>

    @if(Config::get('app.debug'))
        <script src="{{ asset('build/js/vendor/jquery.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-route.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-resource.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-animate.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-messages.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/ui-bootstrap-tpls.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/navbar.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-cookies.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/query-string.js') }}"></script>
        <script src="{{ asset('build/js/vendor/angular-oauth2.min.js') }}"></script>
        <script src="{{ asset('build/js/vendor/ui-bootstrap-tpls.min.js') }}"></script>

        <script src="{{asset('build/js/app.js')}}"></script>

        <!-- Controllers -->
        <script src="{{asset('build/js/controllers/login.js')}}"></script>
        <script src="{{asset('build/js/controllers/home.js')}}"></script>
        <script src="{{asset('build/js/controllers/client/clientList.js')}}"></script>

        <!-- Services -->
        <script src="{{asset('build/js/services/client.js')}}"></script>

    @else
        <script src="{{ elixir('js/all.js') }}"></script>
    @endif

    <!-- Routes -->
    <script src="{{asset('build/js/app.js')}}"></script>

</body>
</html>

文件:app.js

var app = angular.module('app', ['ngRoute', 'ngAnimate', 'angular-oauth2', 'ui.bootstrap.tpls', 'app.controllers', 'app.services']);

angular.module('app.controllers', ['ngMessages', 'angular-oauth2']);
angular.module('app.services', ['ngResource']);

app.provider('appConfig', function () {
   var config = {
       baseUrl: 'http://localhost:8000'
   };

   return{
       config: config,
       $get: function(){
           return config;
       }
   }
});

app.config([
    '$routeProvider', 'OAuthProvider', 'OAuthTokenProvider', 'appConfigProvider',
    function($routeProvider, OAuthProvider, OAuthTokenProvider, appConfigProvider){
    $routeProvider
        .when('/login',{
            templateUrl: 'build/views/login.html',
            controller: 'LoginController'
        })
        .when('/home',{
            templateUrl: 'build/views/home.html',
            controller: 'HomeController'
        })
        .when('/clients',{
            templateUrl: 'build/views/client/list.html',
            controller: 'ClientListController'
        });
    OAuthProvider.configure({
        baseUrl: appConfigProvider.config.baseUrl,
        clientId: 'app',
        clientSecret: 'secret', //optional
        grantPath: 'oauth/access_token' //'oauth/token'
    });
    OAuthTokenProvider.configure({
        name: 'token',
        option:{
            secure: false
        }
    });
}]);

app.run(['$rootScope', '$window', 'OAuth', function ($rootScope, $window, OAuth) {
    $rootScope.$on('oauth:error', function (event, rejection) {
        // Ignore 'invalid_grant' error - should be catched on 'LoginController
        if('invalid_grant' === rejection.data.error){
            return;
        }

        // Refresh token when a 'invalid_token' error occurs
        if('invalid_token' === rejection.data.error){
            return OAuth.getRefreshToken();
        }

        return $window.location.href = '/login?error_reason=' + rejection.data.error;
    });
}]);

档案:home.js

angular.module('app.controllers')
    .controller('HomeController', ['$scope', function ($scope) {

    }]);
javascript angularjs laravel service controller
2个回答
0
投票

可能是因为build/js/app.js被加载两次而发生错误:

  • 第一次在控制器之前加载它
  • 第二次在<!-- Routes -->评论后加载。

尝试删除build/js/app.js的第二个负载,看看它是否是根本原因。


0
投票

如果仔细观察控制台,它实际上会在那里说“HomeController不是一个函数”

我的猜测是(因为我看不到你把它包含在任何地方),你忘了在Angular中定义你的控制器。

我从来没有使用过Angular(或者任何类型的框架),所以我可能会弄错,但是经过一段时间的网络搜索后我才发现:

   function studentController($scope) {
      $scope.student = {
         firstName: "Mahesh",
         lastName: "Parashar",

         fullName: function() {
            var studentObject;
            studentObject = $scope.student;
            return studentObject.firstName + " " + studentObject.lastName;
         }
      };
   }
© www.soinside.com 2019 - 2024. All rights reserved.