Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error when using ng-controller in angularjs ver 1.3.0 [duplicate]

Tags:

angularjs

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?

like image 371
tarzanbappa Avatar asked Oct 30 '14 07:10

tarzanbappa


1 Answers

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";
}
like image 113
Kalhan.Toress Avatar answered Nov 11 '22 20:11

Kalhan.Toress