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

前端之家收集整理的这篇文章主要介绍了在angularjs ver 1.3.0中使用ng-controller时出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Controller not a function,got undefined,while defining controllers globally14个
嗨,我正在关注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>

而且我认为我和教程一样.但是当我用firebug检查时,它给了我以下错误.

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.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";
}
原文链接:https://www.f2er.com/angularjs/141002.html

猜你在找的Angularjs相关文章