错误:[ng:areq] http://errors.angularjs.org/1.4.4/ng/areq?p0=FirstCtrl&p1=not%20a%20function%2C%20got%20undefined?

时间:2015-08-22 09:13:32

标签: javascript angularjs

我是angularjs的新手,想知道我的代码有什么问题。我正在学习一个教程,但是因为这个错误而陷入困境。

的index.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>AngularJS</title>
<link rel="stylesheet" href="foundation.min.css">
<script src="main.js"></script>
</head>
<body>

<div ng-app="">
    <div ng-controller="FirstCtrl">

    <h1>{{data.message + " world"}}</h1>
    <div class="{{data.message}}">
        Wrap me with foundation component
    </div>
    </div>
</div>

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4 
 /angular.min.js"></script>
</body>
</html>

main.js

function FirstCtrl($scope){
  $scope.data = {message: "hello"};
}

1 个答案:

答案 0 :(得分:3)

您必须创建一个模块,如下所示:

var app = angular.module("MyApp",[]);
app.controller("FirstCtrl",function($scope){
    $scope.data = {message: "hello"};
});



<div ng-app="MyApp">
    <div ng-controller="FirstCtrl">

    <h1>{{data.message + " world"}}</h1>
    <div class="{{data.message}}">
        Wrap me with foundation component
    </div>
    </div>
</div>