为什么Angularjs不会检测到我的控制器?

时间:2015-10-22 00:41:50

标签: javascript jquery html angularjs

当我尝试通过angularjs使用控制器时,我不断收到此消息,我不知道为什么它无法检测到我的控制器。

angular.min.js:107Error: [ng:areq] http://errors.angularjs.org/1.4.7/ng/areq?p0=MainController&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:6:416
    at qb (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:22:131)
    at Sa (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:22:218)
    at b.$get (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:80:81)
    at O (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:59:501)
    at K (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:60:338)
    at g (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:54:410)
    at g (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:54:433)
    at g (file:///Users/bradyedgar/Desktop/resources/JS/angular_test/root/js/angular.min.js:54:433)(anonymous function) @ angular.min.js:107b.$get @ angular.min.js:80a.$get.n.$apply @ angular.min.js:133(anonymous function) @ angular.min.js:20e @ angular.min.js:39d @ angular.min.js:19zc @ angular.min.js:20Zd @ angular.min.js:19(anonymous function) @ angular.min.js:293a @ angular.min.js:174c @ angular.min.js:35

我将在下面提供我的代码

<!DOCTYPE html>
<html ng-app>
<head>
<title>Title of the document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/angular.min.js"></script>
<script>

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

    myApp.controller('MainController', ['$scope', function($scope) {
      $scope.message = 'Hello World';
    }]);

</script>
<style type="text/css">
    @import url("css/reset.css");
    @import url("css/style.css");
</style>
</head>

<body>
    <div ng-controller="MainController">
      <h1>{{message}}</h1>
    </div>
</body>

</html>

2 个答案:

答案 0 :(得分:2)

Phil提供的答案

我需要将我的html标签更新为

 <html ng-app="myApp">

答案 1 :(得分:2)

您需要在ng-app指令中指定应用程序的名称:

<html ng-app="myApp">