在angularjs ver 1.3.0中使用ng-controller时出错

时间:2014-10-30 07:03:47

标签: angularjs

您好我关注了angularjs的一些教程

我使用的是angularjs版本1.3.0

这是我的代码

<div ng-app="" ng-controller="personController">
    First Name:
    <input type="text" ng-model="firstName"><br>
    Last Name:
    <input type="text" ng-model="lastName"><br>
    <br>
    Full Name: {{firstName+" " + lastName}}

</div>

<script type="text/javascript">

    function personController($scope) {
        $scope.firstName = "David";
        $scope.lastName = "Silva";
    }
</script>

我认为我和教程一样。但是当我用萤火虫检查时,它给了我以下错误。

Error: [ng:areq] Argument 'personController' is not a function, got undefined 
http://errors.angularjs.org/1.3.0/ng/areq?
p0=personController&p1=not%20a%20function%2C%20got%20undefined

如何摆脱这个?

1 个答案:

答案 0 :(得分:24)

角度1.3.0中的

你必须这样做,因为全局控制器在1.3.0-beta中被禁用。 reference

<div ng-app="myApp" ng-controller="personController">


<script>
var app = angular.module("myApp",[]);

app.controller('personController', function($scope){
     $scope.firstName = "David";
     $scope.lastName = "Silva";
})
</script>

它还说你可以通过使用下面的代码来获得旧行为,但不推荐

<div ng-app="myApp" ng-controller="personController">

var app = angular.module("myApp",[]).config(['$controllerProvider', function($controllerProvider) {
     $controllerProvider.allowGlobals();
}]);

function personController($scope) {
    $scope.firstName = "David";
    $scope.lastName = "Silva";
}