Angular - undefined error

I have just started learning Angular.

Below is some very straightforward code. I am not able to make out which is wrong with the piece of code.

index.html
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
 <!--   <script src="lib/angular.min.js" type="text/javascript"></script> -->
    <script src="script.js" type="text/javascript"></script>
  </head>
  <body>
  	<div ng-controller="MainController">
  		<div>First Name: {{person.firstName}}</div>
  		<div>Last Name: {{person.lastName}}</div>
  	</div>
  </body>
</html>

Open in new window


script.js
 var MainController = function($scope) {
	var person = {
		firstName: "ABC",
		lastName: "XYZ"
	};

	$scope.person = person;
  };

Open in new window


Error in browser console
Error: [ng:areq] http://errors.angularjs.org/1.3.14/ng/areq?p0=MainController&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
    at Sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:19:510)
    at tb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:20:78)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:75:331
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:57:65
    at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:7:408)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:56:443)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:299)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)

Open in new window


As a result, expressions are not evaluated at runtime and this is what I see in the browser

First Name: {{person.firstName}}
Last Name: {{person.lastName}}

Open in new window


Can someone please help me identify the problem.

Thank you
LVL 1
ank5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
since version 1.3.0 you have to explicitly list your controller as Angular does no longer look into the global window object.

Therefore simply change your HTML to name your ng-app:
<html ng-app="yourApp">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
 <!--   <script src="lib/angular.min.js" type="text/javascript"></script> -->
    <script src="script.js" type="text/javascript"></script>
  </head>
  <body>
  	<div ng-controller="MainController">
  		<div>First Name: {{person.firstName}}</div>
  		<div>Last Name: {{person.lastName}}</div>
  	</div>
  </body>
</html>

Open in new window

and change your script to:
var yourApp = angular.module('yourApp',[]);

yourApp.controller('MainController', ['$scope', function($scope) {
	var person = {
		firstName: "ABC",
		lastName: "XYZ"
	};

	$scope.person = person;
}]);

Open in new window


HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.