1

I have the following controller:

<div ng-app="" ng-controller="studentController">
    <table>
        <tr>
            <th>Name</th>
            <th>Marks</th>
        </tr>
        <tr ng-repeat="subject in student.subjects">
            <td> {{ subject.name }} </td>
            <td> {{ subject.marks }} </td>
        </tr>
    </table>
</div>

I have the following script:

function studentController($scope) {
        $scope.student = {
            subjects:[
                {name:'Physics',marks:70},
                {name:'Chemistry',marks:80},
                {name:'Math',marks:65},
                {name:'English',marks:75},
                {name:'Hindi',marks:67}
            ]
        };
    }

In the console the error says that the studentController is not a function. So it could be from the JS but I dont notice anything, ideas?

scniro
  • 16,844
  • 8
  • 62
  • 106
Froy
  • 708
  • 1
  • 6
  • 15

1 Answers1

2

Global controllers are no longer supported; you must define them properly. See SO question: Why doesn't the latest version of angular support global controller functions? for some details on this - since you'll still find orphaned tutorials out there using this old pattern. Observe the following...

angular.module('app', [])
.controller('studentController', function($scope) {
    /*...*/
});

<div ng-app="app" ng-controller="studentController">
    <table>
        <tr>
            <th>Name</th>
            <th>Marks</th>
        </tr>
        <tr ng-repeat="subject in student.subjects">
            <td> {{ subject.name }} </td>
            <td> {{ subject.marks }} </td>
        </tr>
    </table>
</div>

JSFiddle Link - working demo

Community
  • 1
  • 1
scniro
  • 16,844
  • 8
  • 62
  • 106
  • You are correct sir. Looks like the tutorial I am using is from early 2014. Thank you! – Froy Dec 30 '15 at 19:50
  • @FroyT great! Please remember to accept answer if you found helpful and enjoy all the great learning ahead of you :) – scniro Dec 30 '15 at 19:58