2014-11-21 142 views
1

我正在用ng-app创建一个AngularJS控制器,但它无法正常工作。AngularJS错误 - “控制器未定义”或“未定义函数”

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

NG-应用:

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

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

铬控制台

Error: [ng:areq] http://errors.angularjs.org/1.2.17/ng/areq?p0=localizationCtrl&p1=not%20a%20function%2C%20got%20undefined at Error (native) at http://localhost:1914/Scripts/angular.min.js:6:450 at Cb (http://localhost:1914/Scripts/angular.min.js:19:130) at Sa (http://localhost:1914/Scripts/angular.min.js:19:217) at http://localhost:1914/Scripts/angular.min.js:66:451 at http://localhost:1914/Scripts/angular.min.js:53:250 at q (http://localhost:1914/Scripts/angular.min.js:7:386) at N (http://localhost:1914/Scripts/angular.min.js:53:115) at g (http://localhost:1914/Scripts/angular.min.js:47:82) at http://localhost:1914/Scripts/angular.min.js:46:256

+0

两件事情:检查你写脚本标签加载该文件,并尝试对儿童的div移动控制器.. – harishr 2014-11-21 04:44:24

+0

我发现不妥。我测试了角度v1.2.1,所有运行正常。所以角度版本或脚本加载错误有什么问题? – creeper 2014-11-21 04:56:53

+0

我试过两种方法,但问题仍然存在 – 2014-11-21 06:22:34

回答

0

正常工作与所述script inlined: jsFiddle,所以脚本装载定时是可疑,如上述评论。

(顺便说一句,收盘</form>看起来缺少:)

+0

我忘了写在这里 – 2014-11-21 05:20:48

0

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

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

$scope.Save = function (local) { 
    console.log(local); 
} 
}]) 

确定将文件作为ioife包装。

(function(){ 

//---code-- 

})();