angular.js:38未捕获错误:[$ injector:modulerr]

时间:2017-05-15 14:19:53

标签: javascript jquery html angularjs json

我一直收到这个错误:

var app = angular.module('testApp', ['ngAnimate', 'ngSanitize', 'ui.bootstrap','ui.router']);

我的应用程序是在html中定义和调用的,与我的控制器相同。我在这里做错了什么?

app.js:

app.controller('feedCtrl', function($scope, $http) {
    $http.get('https://api.myjson.com/bins/13vg19').then(function(res) {
        $scope.res = res.data;
        console.log($scope.res);
    });
});

ctrl.js:

<!DOCTYPE html>
<html ng-app="testApp">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>My AngularJS App</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="app.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <script src="./components/app/app.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.4/angular-sanitize.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/x2js/1.2.0/xml2json.js"></script>
  <script src="./components/controller/ctrl.js"></script>
</head>
<body>

<div data-ng-controller="feedCtrl">

</div>

</body>
</html>

HTML:

ng-app

这里究竟出现了什么错误?我尝试在body代码中放​​置uint8_t c,结果相同。如您所见,在角度库之后调用app.js.

感谢。

编辑:我刚刚在控制台中注意到,在网络下,根本没有调用https://api.myjson.com/bins/13vg19的请求。

2 个答案:

答案 0 :(得分:6)

在加载所有依赖项引用后加载app.js,您的订单应该是

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.4/angular-sanitize.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/x2js/1.2.0/xml2json.js"></script>
  <script src="./components/app/app.js"></script>
  <script src="./components/controller/ctrl.js"></script>

答案 1 :(得分:1)

此问题意味着angular无法将模块注入您的应用。原因可能是您没有在HTML或版本不匹配中导入它们(例如导入anguler版本x和角度动画版本y)。一般来说,在角度异常中,消息通常包含指向angular网站的链接,以纯文本形式更好地解释问题。

正如我所看到的那样,在导入必要的角度文件之前导入了js文件,并且没有在所有angular-animate和ui.bootstrap中导入。