为什么我的AngularJS控制器定义和标记没有按预期工作?-<控制器>不是函数错误

Why is my AngularJS controller definition and markup not working as expected? - <controller> is not a function error

本文关键字:控制器 lt 错误 函数 工作 gt 定义 AngularJS 我的 为什么      更新时间:2023-09-26

我有以下控制器:

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

我有以下脚本:

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}
            ]
        };
    }

在控制台中,错误表明studentController不是一个函数。所以它可能来自JS,但我没有注意到任何东西,想法?

不再支持全局控制器;你必须正确地定义它们。请参阅SO问题:为什么最新版本的角度控制器不支持全局控制器功能?关于这方面的一些细节-因为你仍然会发现使用这个旧模式的孤立教程。请注意以下内容。。。

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链接-工作演示