1

I have written a simple program in AngularJS after including the minified angular JS in the script tag but the browser is not able to parse the angular JS code. I have other programs with more or less same code and they are working fine.

Am I missing/overlooking anything ?

MVC Example

<!DOCTYPE html>
<html ng-app>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min.js"></script>
        <script>
            function MyFirstCtrl($scope) {
                var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
                $scope.ourEmployees = employees;
            }
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>

Browser Output:

Number of Employees: {{ourEmployees.length}}
Yogeshree Koyani
  • 1,649
  • 10
  • 19
Nital
  • 5,784
  • 26
  • 103
  • 195
  • 2
    global functions no longer supported - See http://stackoverflow.com/questions/25111831/controller-not-a-function-got-undefined-while-defining-controllers-globally – charlietfl Sep 29 '15 at 16:20

3 Answers3

4

You need to create a module to use with ng-app.

angular.module("foo", []);
angular.controller("MyFirstCtrl", MyFirstCtrl);

<html ng-app=foo>
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You need to start the app and add the controller in the right form:

<script>
var app = angular.module("app", []);
app.controller('MyFirstCtrl', ['$scope', function ($scope) {
    var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
    $scope.ourEmployees = employees;
}]);
<script>
michelem
  • 14,430
  • 5
  • 50
  • 66
0

After some R&D I finally figured out solution for both v1.2 and v1.3+

v1.2

<!DOCTYPE html>
<html ng-app=foo>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min-1.2.js"></script>
        <script>
            angular.module("foo", []);
            angular.controller("MyFirstCtrl", MyFirstCtrl);

            var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];

            function MyFirstCtrl($scope) {
                var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
                $scope.ourEmployees = employees;
            }
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>

v1.3+ Onwards

<!DOCTYPE html>
<html ng-app=foo>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min-1.4.js"></script>
        <script>
            var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
            var foo = angular.module("foo", []);
            foo.controller("MyFirstCtrl", function ($scope) {
                $scope.ourEmployees = employees;
            });
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>
Nital
  • 5,784
  • 26
  • 103
  • 195