Getting error when using ng controller in angularjs version 1.3.0

Hi, I am following some angularjs tutorials

I am using 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 did the same as the textbook. But this gives me the 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?

+6
source share
1 answer

in angular 1.3.0 u will have to do as shown below, since global controllers are disabled in the 1.3.0 beta. link

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

He also said that you can get older behavior using the code below , but its not recommended

 <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"; } 
+24
source

Source: https://habr.com/ru/post/977457/


All Articles