angular app.controller引发错误

时间:2013-08-01 12:06:55

标签: angularjs

我正在学习AngularJS并试图做基本的事情:

<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"     type="text/javascript"></script>

<script type="text/javascript">
    var myapp = angular.module('MyTutorialApp',[]);

    myapp.controller("MyMainController", function($scope){
        $scope.understand = "I now understand how the scope works2!";
    });
</script>
</head>
<body ng-app='MyTutorialApp'>
    <div id='content' ng-controller='MyMainController'>
        {{understand}}
    </div>
</body>
</html>

但我收到上面代码的错误,错误说“错误:参数'MyMainController'不是函数,未定义”

但如果我将使用下一个代码,该应用程序将正常工作

function MyMainController($scope) {
    $scope.understand = 'I now understand how the scope works3';
}

1 个答案:

答案 0 :(得分:3)

那是因为你的html标签必须像:

<html ng-app="MyTutorialApp">