Angular.js只加载一半的时间

Angular.js only loading half the times

本文关键字:时间 一半 加载 js Angular      更新时间:2023-09-26

我遇到了一个angular应用程序的问题。我正在使用凉亭。当我启动服务器时,我有时会得到这个错误:

未捕获错误:[$injector:modulerr]实例化模块myApp失败,原因是:错误:[$injector:modulerr]实例化模块ui失败。路由器由于:错误:[$injector:nomod]模块的ui。路由器不可用!您要么拼错了模块......2)

然后我有时会得到这个错误:

Uncaught SyntaxError: Unexpected identifier angular.js:573

Uncaught TypeError: Cannot read property 'module' of undefined

Uncaught SyntaxError: Unexpected token ILLEGAL

Uncaught ReferenceError: angular is not defined ui-bootstrap-tpls-0.11.0.js:8

Uncaught ReferenceError: angular is not defined app.js:4

Uncaught ReferenceError: angular is not defined

有时css也不能完全加载,但不会得到控制台错误。其他时候它工作得很好。

这是我的代码设置
<body ng-app="myApp" class="ng-scope">
<div>
  <div ui-view>
  </div>
</div>
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script> 
  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
  <script src="js/create.js"></script>
  <script src="js/view.js"></script>
  <script src="js/header.js"></script>
  <script src="js/form-service.js"></script>
  <script src="js/form-directive.js"></script>
  <script src="js/field-directive.js"></script>
  <script src="js/jquery.dcjqaccordion.2.7.js"></script>
</body>

这是我的应用程序所在的app.js

var myApp = angular.module('myApp', [
  'ui.bootstrap.tabs',
  'ui.bootstrap.accordion',
  'ui.bootstrap.collapse',
  /*
  '$strap.directives',
  */
  'ui.router',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'

]);

不太确定该做什么。我以为加载angular然后在一段时间后加载其他脚本会修复它,但这行不通。

您的问题是没有使用绝对url到.js文件。在每个路径前包含一个/,它应该可以解决您的问题。例如:

    <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="bower_components/angular-ui-router/release/angular-ui-router.js">

<script src="/bower_components/angular/angular.js"></script>
  <script src="/bower_components/angular-route/angular-route.js"></script>
  <script src="/bower_components/angular-ui-router/release/angular-ui-router.js">