我刚刚开始学习angularJS,我的第一个控制器出了问题。
这是我的HTML:
<!DOCTYPE html>
<html ng-app="">
<head lang="en">
<title></title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="includes/main.js"></script>
</head>
<body>
<div ng-controller="firstCtrl">
<h1>{{data.message +" world"}}</h1>
<div class="{{data.message}}">
This is just a game!
</div>
</div>
</body>
</html>
这是javascript文件main.js:
function firstCtrl ($scope){
$scope.data = {message: "Hello"};
}
在教程我正在使用我想在h1中看到它的Hello World消息,但我实际上看到了它:
{{data.message +" world"}}
这只是一场游戏!
这是控制台中的错误:
Error: [ng:areq] http://errors.angularjs.org/1.3.8/ng/areq?p0=firstCtrl&p1=not%20a%20function%2C%20got%20undefined
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:6:416
at Qb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:19:417)
at sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:20:1)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:76:95
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:57:257
at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:7:408)
at v (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:57:124)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:52:9)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:52:26)
有人可以帮我弄清楚它是什么? 我复制了教程中的确切代码,所以我想我的配置,但我不知道它是什么
答案 0 :(得分:3)
问题出在您的控制器上。尝试像这样更换你的控制器
var myApp = angular.module('***',[]);
myApp.controller('firstCtrl', ['$scope', function($scope) {
$scope.data = {message: "Hello"};
}]);
答案 1 :(得分:0)
好的,我在这里找到答案:[Controller not a function, got undefined, while defining controllers globally
Igor Semin - thx指南