AngularJS错误 - “控制器未定义”或“未定义函数”

AngularJS errors - "controller is not defined" or "not a function got undefined"

本文关键字:未定义 函数 未定义函数 控制器 错误 AngularJS      更新时间:2023-09-26

我正在使用ng-app创建一个AngularJS控制器,但它无法正常工作。

在Chrome控制台中,我收到错误angular ctrl is not definednot a function got undefined

ng-app:

var localizationModule = angular.module("localizationModule",[]);

控制器.js

 localizationModule.controller('localizationCtrl', ['$scope',
function ($scope) {
$scope.local = {
   ProjectSingular: '',
    ProjectPlural: '',
    ServiceObjectSingular: '',
    ServiceObjectPlural: '',
    ServicePictureSingular: '',
    ServicePicturePlural: '',
}
$scope.Save = function (local) {
    console.log(local);
}
}])

.html

<form ng-app="localizationModule" ng-controller="localizationCtrl">
<div class="panel panel-info">
    <div class="panel-heading">
        <strong>Localization Setting</strong>
    </div>
    <div class="panel-body">
        <div class="row">
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="table-responsive">
                    <table class="table table-striped table-hover table-bordered">
                        <tr>
                            <th>Elements</th>
                            <th>Singular</th>
                            <th>Plural</th>
                        </tr>
                        <tr>
                            <td>Project</td>
                            <td><input type="text" required ng-model="local.ProjectSingular" class="form-control" /></td>
                            <td><input type="text" required ng-model="local.ProjectPlural" class="form-control" /></td>
                        </tr>
                        <tr>
                            <td>Service Object</td>
                            <td><input type="text" required ng-model="local.ServiceObjectSingular" class="form-control" /></td>
                            <td><input type="text" required ng-model="local.ServiceObjectPlural" class="form-control" /></td>
                        </tr>
                        <tr>
                            <td>Service Picture</td>
                            <td><input type="text" required ng-model="local.ServicePictureSingular" class="form-control" /></td>
                            <td><input type="text" required ng-model="local.ServicePicturePlural" class="form-control" /></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-heading">
        <input type="submit" class="btn btn-success pull-right" value="Save" />
        <div class="clearfix"></div>
    </div>
</div>
</form>

Chrome 控制台

错误: [ng:areq] http://errors.angularjs.org/1.2.17/ng/areq?p0=localizationCtrl&p1=not%20a%20function%2C%20got%20undefined 错误(本机) 在 http://localhost:1914/Scripts/angular.min.js:6:450 在 Cb (http://localhost:1914/Scripts/angular.min.js:19:130) 在萨(http://localhost:1914/Scripts/angular.min.js:19:217) 在 http://localhost:1914/Scripts/angular.min.js:66:451 在 http://localhost:1914/Scripts/angular.min.js:53:250 在 Q (http://localhost:1914/Scripts/angular.min.js:7:386) 在 N (http://localhost:1914/Scripts/angular.min.js:53:115) 处 在 G (http://localhost:1914/Scripts/angular.min.js:47:82) 在 http://localhost:1914/Scripts/angular.min.js:46:256

与内联脚本一起工作:jsFiddle,因此脚本加载的时间是可疑的,如上所述。

(顺便说一句,结尾</form>看起来不见了:)

我注意到角度中的一个小错误。是否在与控制器相同的文件中具有模块的声明?Id 尝试在每个控制器文件中声明模块,但没有依赖注入数组。

angular.module("localizationModule").controller('localizationCtrl', ['$scope',
function ($scope) {
$scope.local = {
   ProjectSingular: '',
    ProjectPlural: '',
    ServiceObjectSingular: '',
    ServiceObjectPlural: '',
    ServicePictureSingular: '',
    ServicePicturePlural: '',
}
$scope.Save = function (local) {
    console.log(local);
}
}])

Bes 也一定要将文件包装为 ioife。

(function(){
//---code--
})();