Alex Clarke
Alex Clarke

Reputation: 1

Angular JS controller not working

I am just starting out with AngulasJS, and when I run this code, I am getting the error "Argument 'teacherCtrl' is not a function, got undefined", but it is clearly defined. Please help.

<body>
    <script>
        function teacherCtrl($scope) {
            $scope.teachers = [
                { name: 'Bill Melega', subject: 'History'},
                { name: 'Top Stanfa', subject: 'English'},
                { name: 'Mike Bremer', subject: 'English'}
            ];
        }
    </script>
    Name: <input type="text" ng-model="search"> {{search}}

    <table ng-controller="teacherCtrl">
        <tr ng-repeat="teacher in teachers | filter:search">
            <td ng-repeat="property in teacher">
                {{ property }}
            </td>
        </tr>
    </table>

</body>

Upvotes: 0

Views: 1266

Answers (1)

Shomz
Shomz

Reputation: 37701

Provided that you have set up ng-app, here's the reason: in Angular 1.3+ you can't define controllers like that anymore. You need the whole thing: app.controller(....

It all comes down to how you create controllers. Luckily the fix is the way you should be writing your controllers (though you can opt into the old behavior). When I’ve taught AngularJS in the past, I’ve created controllers using global functions as that makes the story simple. Usually when I’ve taught it before, I’m trying to help people understand the concepts, not the best practices. Starting with AngularJS 1.3, this isn’t the default behavior.

Read more about it here: http://wildermuth.com/2014/11/11/Angular_1_3_and_Breaking_Change_for_Controllers

Upvotes: 1

Related Questions