【问题标题】:angular app.controller raises an error角度 app.controller 引发错误
【发布时间】:2013-08-01 12:06:55
【问题描述】:

我正在学习 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';
}

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    那是因为你的html标签需要是这样的:

    <html ng-app="MyTutorialApp">
    

    【讨论】:

    • 哇,我怎么错过了?我可能很累了:),谢谢你的回答
    • 是的! :) 你也可以从body标签中删除它。
    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多