javascript 为什么我在这里收到 Angular $injector:modulerr 错误?使用 1.5 测试版

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33468075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:31:24  来源:igfitidea点击:

Why am I getting the Angular $injector:modulerr error here? Using 1.5 beta

javascriptangularjsangularjs-controllerangularjs-module

提问by Leon Gaban

The markup

标记

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Angular I</title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="author" href="humans.txt">
</head>
<body ng-app="app">

    <section ng-controller="mainCtrl">
      <h1>My App</h1>
      <p>{{name}}</p>
    </section>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-route.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-animate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-resource.min.js"></script>

    <script src="script.js"></script>
</body>
</html>

The script.js

脚本.js

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

app.controller('mainCtrl', function($scope) {
    $scope.name = "Leon Gaban";
})

I'm including angular.min, angular-route and even angular-resource and animate...

我包括 angular.min、angular-route 甚至 angular-resource 和 animate...

回答by Pankaj Parkar

You had wrong ng-appmodule, it should be ng-app="friendsList"instead of app.

你有错误的ng-app模块,它应该是ng-app="friendsList"而不是app.

The error is not due to angular versions, it is because you are injecting wrong dependency in your app mdoule. Simply you can not inject mainCtrlinside your module as its controller mainCtrlwhich you are going to register with your module.

该错误不是由于 angular 版本,而是因为您在应用程序 mdoule 中注入了错误的依赖项。简单地说,您不能将mainCtrl模块注入到您mainCtrl要向模块注册的控制器中。

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

Also you should have ng-controller="mainCtrl"on html.

你也应该有ng-controller="mainCtrl"html。

<section ng-controller="mainCtrl">

</section>