简体   繁体   English

AngularJS:未捕获ReferenceError:未定义angular app.js:1(匿名函数):

[英]AngularJS: Uncaught ReferenceError: angular is not defined app.js:1(anonymous function):

I have moved my AngularJS test project into app.js, controllers.js and Services.js. 我已经将AngularJS测试项目移至app.js,controllers.js和Services.js。 After it my project is not running and not even a single page is being working properly. 之后,我的项目没有运行,甚至没有一个页面正常工作。

i have attached my main source files here along with exception screenshot 我在此处附加了我的主要源文件以及异常屏幕截图

index.html index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">

        <!-- bootstrap App.js -->
        <script src="includes/assets/app.js"></script>

        <script src="includes/angular.js"></script>
        <script src="includes/angular-ui-router.js"></script>

        <script src="includes/ui-bootstrap-0.11.0.js"></script>
        <script src="includes/ui-bootstrap-tpls-0.11.0.js"></script>


        <script src="js/CalculatorService.js"></script>
        <script src="js/Controller.js"></script>
        <script src="js/app.js"></script>

        <link rel="stylesheet" type="text/css" href="includes/assets/bootstrap.css" />
        <!-- demo.css is related to model window -->
        <link rel="stylesheet" type="text/css" href="includes/assets/demo.css" />

        <title>AngularJS Tutorial</title>
        </head>

        <body ng-app="app">
        <div width="100%">
        <!-- NAVIGATION -->
        <nav class="navbar navbar-inverse" role="navigation" title="Navigation Bar">
            <div class="navbar-header">
                <a class="navbar-brand" ui-sref="#">AngularJS Tutorial</a>
            </div>
            <ul class="nav navbar-nav">
                <li><a ui-sref="home">Home</a></li>
                <li><a ui-sref="model-window">Model Window</a></li>
                <li><a ui-sref="simple-form">Simple Form</a></li>
                <li><a ui-sref="basic-form-validation">Basic Form validation</a></li>
                <li><a ui-sref="calculater">Calculator</a>
            </ul>
        </nav>
        </div>


        <!-- MAIN CONTENT -->
        <div class="container" width="100%" style='padding-top: 50px'>

            <!-- THIS IS WHERE WE WILL INJECT OUR CONTENT ============================== -->
            <div ui-view></div>

        </div>
        </html>

app.js app.js

var app = angular.module('app', [ 
                         'ui.router', 
                         'ui.bootstrap',
                         'app.controllers',
                         'app.services'
                       ]);

app.config(function($stateProvider, $urlRouterProvider) {
    console.log('in app config...');
    $urlRouterProvider.otherwise('/home');

    $stateProvider
                .state('home', {
                        url : '/home',
                        templateUrl : 'home.html'
                })

                .state('model-window', {
                        url : '/model-window',
                        templateUrl : 'model-window.html',
                        controller: 'ModalDemoCtrl'
                })

                .state('simple-form', {
                    url: '/simple-form',
                    templateUrl: 'simple-form.html',
                    controller: 'SimpleFormCtrl'
                })

                .state('basic-form-validation', {
                        url: '/basic-form-validation',
                        templateUrl: 'basic-form-validation.html',
                        controller: 'BasicFormValidationCtrl'
                })

                .state('calculater' , {
                    url: '/calculator',
                    templateUrl: 'calculator.html',
                    controller: 'CalculatorCtrl'

                });

}); // closes $app.config()

//let's define the scotch controller that we call up in the about state
app.controller('ModalDemoCtrl', function($scope, $modal) {

    console.log('in app controller...');
     // code for bootstrap angular-ui
    $scope.items = ['item1', 'item2', 'item3'];

      $scope.open = function (size) {

        var modalInstance = $modal.open({
          templateUrl: 'myModalContent.html',
          controller: ModalInstanceCtrl,
          size: size,
          resolve: {
            items: function () {
              return $scope.items;
            }
          }
        });

        modalInstance.result.then(function (selectedItem) {
          $scope.selected = selectedItem;
        }, function () {
          $log.info('Modal dismissed at: ' + new Date());
        });
      };
    var ModalDemoCtrl = function ($scope, $modal) {


        };

        // Please note that $modalInstance represents a modal window (instance) dependency.
        // It is not the same as the $modal service used above.

        var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

          $scope.items = items;
          $scope.selected = {
            item: $scope.items[0]
          };

          $scope.ok = function () {
            $modalInstance.close($scope.selected.item);
          };

          $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
          };
        };

});

// Start of a SimpleFormController
app.controller('SimpleFormCtrl', ['$scope', function($scope) {
    $scope.master = {};

    $scope.update = function(user) {
      $scope.master = angular.copy(user);
    };

    $scope.reset = function() {
      $scope.user = angular.copy($scope.master);
      $scope.master = {};
    };

    $scope.reset();
  }]); // End of SimpleFormController

app.controller('BasicFormValidationCtrl', ['$scope', function($scope) {
     $scope.master = {};

        $scope.update = function(user) {
          $scope.master = angular.copy(user);
        };

        $scope.reset = function() {
          $scope.user = angular.copy($scope.master);
        };

        $scope.isUnchanged = function(user) {
          return angular.equals(user, $scope.master);
        };

        $scope.reset();
}]);

CalculatorService.js CalculatorService.js

    'use strict';

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

    console.log('in angular-tutorial-app Server...');


    app.service('MathService', function() {
        this.add = function(a, b) { return a + b };

        this.subtract = function(a, b) { return a - b };

        this.multiply = function(a, b) { return a * b };

        this.divide = function(a, b) { return a / b };
    });

    app.service('CalculatorService', function(MathService){

        this.square = function(a) { return MathService.multiply(a,a); };
        this.cube = function(a) { return MathService.multiply(a, MathService.multiply(a,a)); };

    });

Controller.js Controller.js

'use strict';

var app = angular.module('app.controllers', ['$scope', '$http', 'CalculatorService']);

app.controller('CalculatorCtrl', function($scope, $http, CalculatorService){

            console.log('in CalculatorCtrl...');
            doSquare = function($scope, CalculatorService) {
                $scope.answer = CalculatorService.square($scope.number);
            }

            doCube = function($scope, CalculatorService) {
                $scope.answer = CalculatorService.cube($scope.number);
            }

        });

Detail Exception when application runs on server: 当应用程序在服务器上运行时,详细异常:

![screen shot of error and resultant page][1]

在此处输入图片说明

calculator.html Calculator.html

<div ng-controller="CalculatorCtrl">
        Enter a number:
        <input type="number" ng-model="number" />
        <button ng-click="doSquare()">X<sup>2</sup></button>
        <button ng-click="doCube()">X<sup>3</sup></button>

        <div>Answer: {{answer}}</div>
    </div>

This is because you have mixed the array notation for injected dependencies in your controller and in your module. 这是因为您已经在控制器和模块中混合了用于注入依赖项的数组符号。

Controller.js Controller.js

'use strict';

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

app.controller('CalculatorCtrl', ['$scope', '$http', 'CalculatorService', function($scope, $http, CalculatorService){

   console.log('in CalculatorCtrl...');
    $scope.doSquare = function() {
     $scope.answer = CalculatorService.square($scope.number);
   };

    $scope.doCube = function() {
     $scope.answer = CalculatorService.cube($scope.number);
   };

}]);

UPDATE: Remove $scope and CalculatorService in functions doSquare and doCube , they are already included as dependencies in your controller. 更新:删除函数doSquaredoCube $scopeCalculatorService ,它们已作为依赖项包含在您的控制器中。 Updated version above. 上面的更新版本。

You can fix it by 您可以通过修复它

'use strict';

var app = angular.module('app.controllers');

var CalculatorCtrl =function($scope, $http, CalculatorService){

            console.log('in CalculatorCtrl...');
            doSquare = function($scope, CalculatorService) {
                $scope.answer = CalculatorService.square($scope.number);
            }

            doCube = function($scope, CalculatorService) {
                $scope.answer = CalculatorService.cube($scope.number);
            }

        };

CalculatorCtrl.$inject = ['$scope', '$http', 'CalculatorService']; //old technique to add dependencies

app.controller('CalculatorCtrl', CalculatorCtrl);

I got the same error: 我遇到了同样的错误:

Uncaught ReferenceError: getList is not defined 未捕获的ReferenceError:未定义getList

Note: In your case, the error says Angular is not defined, but for me, a function could not be find. 注意: 在您的情况下,错误提示未定义Angular,但对我而言,找不到函数。

that was because I forgot a simple thing, my code was like this: 那是因为我忘记了一件简单的事情,我的代码是这样的:

 <a onclick="getList();"> Get List</a>

I called the Angular function by onclick , and it should be ng-click , in my case, I just needed to change that to this: 我通过onclick调用了Angular函数,它应该是ng-click ,在我的情况下,我只需要将其更改为:

 <a ng-click="getList();"> Get List</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError:未定义角度app.js - Uncaught ReferenceError: angular is not defined app.js 未捕获的ReferenceError:app.js中未定义require:3 - Uncaught ReferenceError: require is not defined at app.js:3 未捕获的ReferenceError:未定义角度(匿名函数) - Uncaught ReferenceError: angular is not defined (anonymous function) Laravel Uncaught ReferenceError webpackJsonp未在app.js:1中定义 - Laravel Uncaught ReferenceError webpackJsonp is not defined at app.js:1 app.js:92未捕获的ReferenceError:未定义newCalculation - app.js:92 Uncaught ReferenceError: newCalculation is not defined app.js:63 Uncaught ReferenceError: GLTFLoader is not defined - app.js:63 Uncaught ReferenceError: GLTFLoader is not defined 未捕获的ReferenceError:未定义$(匿名函数) - Uncaught ReferenceError: $ is not defined(anonymous function) 未捕获的ReferenceError:在Angularjs中未定义app - Uncaught ReferenceError: app is not defined in Angularjs 未捕获的ReferenceError:函数未定义(匿名函数) - Uncaught ReferenceError: function not defined (anonymous function) 将使用 CDN 链接创建的 React App 导入 App.js 时出现“未捕获的 ReferenceError:require is not defined” - “Uncaught ReferenceError: require is not defined” when importing into App.js for React App created using CDN links
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM