我正在尝试使用angularJS在我的应用程序(java-ee)中构建前端。我无法将角度应用程序注入到我的jsp文件中。我总是遇到此错误:ReferenceError: angular is not defined。我知道这很容易出错,但找不到解决方案。下面是我的代码:

index.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>

    <head>
        <script src="<c:url value="/resources/angular-app/registration/registration.module.js" />"></script>
        <script src="<c:url value="/resources/angular-app/registration/RegistrationCtrl.js" />"></script>

        <script src="<c:url value="/resources/angular-app/bower_components/jquery/dist/jquery.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular/angular.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-animate/angular-animate.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-route/angular-route.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-resource/angular-resource.js" />"></script>
    </head>

    <body ng-app="frontendApp">
        <div ng-view></></div>
    </body>

</html>


app.js:

'use strict';
/**
 *
 */

angular.module('frontendApp', ['ngRoute', 'registrationControllers'])
    .config([ '$routeProvider', function ($routeProvider){
    $routeProvider
    .when('/rest',{
        templateUrl: 'resources/angular-app/registration/registration.html',
        controller: 'RegistrationCtrl'
    });
}]);


控制器:

'use strict';
/**
 *
 */

var registrationControllers = angular.module('frontendApp', []);

registrationControllers.controller('RegistrationCtrl',['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

最佳答案

如下更改脚本引用。

        <script src="<c:url value="/resources/angular-app/bower_components/jquery/dist/jquery.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular/angular.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-animate/angular-animate.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-route/angular-route.js" />"></script>
        <script src="<c:url value="/resources/angular-app/bower_components/angular-resource/angular-resource.js" />"></script>

        <script src="<c:url value="/resources/angular-app/registration/registration.module.js" />"></script>
        <script src="<c:url value="/resources/angular-app/registration/RegistrationCtrl.js" />"></script>


您在controller.js中覆盖了frontendApp
那就是清除整个frontendApp。您应该始终使用依赖项声明一次模块,然后仅通过该模块名称重用它。

'use strict';

var frontendApp = angular.module('frontendApp', ['ngRoute'])
    .config([ '$routeProvider', function ($routeProvider){
    $routeProvider
    .when('/rest',{
        templateUrl: 'resources/angular-app/registration/registration.html',
        controller: 'RegistrationCtrl'
    });
}]);


控制者

'use strict';

frontendApp.controller('RegistrationCtrl',['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

关于java - Angular 开始(ReferenceError:未定义 Angular ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28285691/

10-12 23:29