【问题标题】:Error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined错误:[ng:areq] 参数 'MyCtrl' 不是函数,未定义
【发布时间】:2014-09-15 17:56:57
【问题描述】:

我是 Angularjs 的新手,我正在学习一个教程,但我得到了标题中的错误。

HTML 代码:

<div data-ng-app="myApp">
    <div data-ng-controller="MyCtrl">
    <form>
        <table>
        <tr style="font-weight: bold">
          <td>ID</td>
          <td>Name</td>
          <td>Surname</td>
          <td>House</td>
          <td>Address</td>
          <td>Locality</td>
          <td>Contact</td>
          <td>Contact 2</td>
          <td>Contact 3</td>
          <td>Reply</td>
          <td>Edit</td>
        </tr>
        <tr data-ng-repeat="person in persons">
            <td>{{person.ID}}</td>
            <td>{{person.Name}}</td>
            <td>{{person.Surname}}</td>
            <td>{{person.House}}</td>
            <td>{{person.Address}}</td>
        </tr>


        </table>
    </form>
    </div>
</div>

<script type="text/javascript">
    //Defining a Angular module
    var myApp = angular.module('myApp', []); 
    //Defining a Angular Controller 
    myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) { 

    //Retrieving the List of people 
    GetPersons(); 
    //Displaying the Save button 
    $scope.DisplaySave = true; 

    function GetPersons() { 
    //Defining the $http service for getting the people 
    $http({
     method: 'GET', url: '/api/data' 
     }). 
     success(function (data) {
      if (data != null || data != 'undefined') { 
        //Assigning people data to the $scope variable
        $scope.persons = data; 
        //Clearing the Person object in create context and Showing default Gender(Male) Checked 
        $scope.newperson = { 
        Id: ''
        }; 
        } 
        }) 
        .error(function (error) { 
            //Showing error message 
            $scope.status = 'Unable to retrieve people' + error.message; 
        });
    }
} ]);
</script>

API 控制器:

public class DataController : ApiController
    {
        //GET api/data

        public IEnumerable<CommonLayer.Telesales> GetPeople()
        {
            return new BusinessLayer.Telesales().getUserSession(User.Identity.Name).AsEnumerable();
        }

更多错误详情:

magicplayer:init: set version: 1.0.1
adme: onDOMStart: got code: user_key=f52009a2292c2b524ac9af2801caef4c443d7cdc7697dff171f77b3c81cd26fa gender=1 age=4
Error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.18/ng/areq?p0=MyCtrl&p1=not%20a%20function%2C%20got%20undefined
    at http://localhost:12570/Scripts/angular.js:78:12
    at assertArg (http://localhost:12570/Scripts/angular.js:1475:11)
    at assertArgFn (http://localhost:12570/Scripts/angular.js:1485:3)
    at http://localhost:12570/Scripts/angular.js:7198:9
    at http://localhost:12570/Scripts/angular.js:6592:34
    at forEach (http://localhost:12570/Scripts/angular.js:327:20)
    at nodeLinkFn (http://localhost:12570/Scripts/angular.js:6579:11)
    at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6028:13)
    at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6031:13)
    at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6031:13)
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:12570/Images/accent.png
onMessageFromBackground: method=statPixel

我做错了什么?我搜索其他解决方案,但似乎我的问题有点不同。

【问题讨论】:

  • 你的控制器在哪里命名为“MyCtrl”???
  • 是的............
  • @KartikeyaKhosla 我尝试将名称从 MyCtrl 更改为 Data 但仍然是错误
  • 不完全确定我改变了什么(除了删除循环中人员的“s”),但它似乎正在工作:JSBin
  • @Goodzilla 是的,我忘记删除我对我所做的“s”,但我仍然得到同样的错误。错误发生在 myApp.controller( 行,因为 GetPersons() 甚至没有被调用

标签: javascript asp.net-mvc angularjs


【解决方案1】:

原来我在布局页面中有以下html标签,它返回了这个错误

<html lang="en ng-app">

从标签中删除了 ng-app,代码完美运行

【讨论】:

    【解决方案2】:

    在我的例子中,我刚刚忘记在 index.html 中添加一个控制器脚本

    【讨论】:

      【解决方案3】:

      当我拼错控制器名称时,我遇到了这种错误。 js文件中大写,html文件的ng-controller标签中没有。

      【讨论】:

        【解决方案4】:

        当我们没有将模块/控制器正确绑定到 DOM 时,有时可能会发生这种情况。我遇到了同样的错误,我发现我的 ng-app 被分配了空白

        <html ng-app="">
        

        我需要提供模块名称。我付出了,它奏效了!

        <html ng-app="app">
        

        【讨论】:

          【解决方案5】:

          我在 Angularjs 控制器中使用 Material Design 对话框时遇到了这个错误,而且很难找到错字:

          templateUrl: 'template.html';
          

          当我用正确的逗号替换分号时,它解决了问题。

          templateUrl: 'template.html',
          

          【讨论】:

          • 引导我解决我的问题。谢谢!
          • np。错别字可能会令人沮丧!
          【解决方案6】:

          有时我们在工厂、控制器和主模块中使用相同的模块名称。所以, 这是它会给出错误的主要原因 Error: [ng:areq] Argument,don't use same module name when you call your services,controllers,factory.

          【讨论】:

            【解决方案7】:

            在检查并仔细检查了所有建议后,我为此让自己发疯了几个小时。但是,事实证明,一旦我清除了我的 Chrome 浏览器数据(cookie、托管应用程序数据以及缓存的图像和文件),错误就消失了。即使刷新似乎也不足以清除所有列出的项目。

            【讨论】:

              【解决方案8】:

              我使用 &lt;html ngapp&gt; 和 angular 1.4.8

              改成 1.2.8 就解决了这个错误。

              我的 html 有 &lt;div class ="container" ng-controller="AppCtrl"&gt;

              我有一个带有该函数的 controller.js 文件

              function AppCtrl(){ console.log("Hello from controller") }

              【讨论】:

                【解决方案9】:

                就我而言:

                • 错误地我有两个身体标签&lt;body ng-controller="CtrlWasNotFound"&gt; &lt;body&gt;&lt;/body &lt;/body&gt;
                • 不仅如此 - app.js 返回 index.html 的 HTML(使用 gulp 并且有配置问题

                【讨论】:

                  【解决方案10】:

                  为了解决这个问题,我不得不发现我在 Angular 路由的声明中拼错了控制器的名称:

                  .when('/todo',{
                              templateUrl: 'partials/todo.html',
                              controller: 'TodoCtrl'
                          })
                  

                  【讨论】:

                    【解决方案11】:
                    <sript
                    

                    而不是

                    <script 
                    

                    ;) 链接 controller.js 时。就我而言

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-09-18
                      • 2015-11-03
                      • 2016-10-23
                      • 2015-10-10
                      • 2016-11-12
                      • 2016-03-04
                      相关资源
                      最近更新 更多