在 1.3.0 版 angularjs 中使用 ng-controller 时出错 [英] Getting an error when using ng-controller in angularjs ver 1.3.0

查看:23
本文介绍了在 1.3.0 版 angularjs 中使用 ng-controller 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 angularjs 的一些教程

我使用的是 angularjs 版本 1.3.0

这是我的代码

名:<input type="text" ng-model="firstName"><br>姓:<input type="text" ng-model="lastName"><br><br>全名:{{firstName+" " + lastName}}

<script type="text/javascript">函数 personController($scope) {$scope.firstName = "大卫";$scope.lastName = "席尔瓦";}

而且我认为我已经完成了与教程相同的操作.但是当我检查 firebug 时,它给了我以下错误.

错误:[ng:areq] 参数personController"不是函数,未定义http://errors.angularjs.org/1.3.0/ng/areq?p0=personController&p1=not%20a%20function%2C%20got%20undefined

如何摆脱这个?

解决方案

在 angular 1.3.0 中你必须像下面那样做,因为全局控制器在 1.3.0-beta 中被禁用.参考


<小时>

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

var app = angular.module("myApp",[]).config(['$controllerProvider', function($controllerProvider) {$controllerProvider.allowGlobals();}]);函数 personController($scope) {$scope.firstName = "大卫";$scope.lastName = "席尔瓦";}

Hi I'm following some tutorials of angularjs

I'm using the angularjs version 1.3.0

here is my code

<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>

And I think I have done as same as the tutorial. But it gives me following error when I check with 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

How to get rid of this?

解决方案

in angular 1.3.0 u have to do like below, Because Global controllers were disabled in 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>


It also said that you can get the older behavior by using below code , but its not recomended

<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";
}

这篇关于在 1.3.0 版 angularjs 中使用 ng-controller 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆