错误:[ng:areq]参数'MyCtrl'不是函数,未定义

时间:2014-07-24 08:56:42

标签: javascript asp.net-mvc angularjs

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

HTML code:

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

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
我做错了什么?我搜索其他解决方案,但似乎我的问题有点不同。

11 个答案:

答案 0 :(得分:8)

原来我在布局页面中有以下html标签返回此错误

<html lang="en ng-app">

从标签中删除了ng-app,代码运行正常

答案 1 :(得分:5)

在我的情况下,我忘记了将一个控制器脚本添加到index.html

答案 2 :(得分:3)

当我拼错我的控制器名称时,我遇到了这种错误。它在js文件中大写,但不在html文件中的ng-controller标记中。

答案 3 :(得分:2)

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

<html ng-app="">

我需要提供模块名称。我给了它并且它有效!

<html ng-app="app">

答案 4 :(得分:2)

我在Angularjs控制器中使用Material Design对话框时出现此错误,但很难找到拼写错误:

templateUrl: 'template.html';

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

templateUrl: 'template.html',

答案 5 :(得分:1)

有时我们在工厂,控制器和主模块中使用相同的模块名称。所以, 这是它会给出错误的主要原因错误:[ng:areq]参数,当你打电话给你的服务,控制器,工厂时,不要使用相同的模块名称。

答案 6 :(得分:1)

在检查并仔细检查所有建议后,我开车疯了几个小时。但事实证明,一旦我清除了我的Chrome浏览器数据(Cookie,托管的应用数据和缓存的图片和文件),错误就会消失。即使刷新也似乎没有清除所有列出的项目。

答案 7 :(得分:0)

我使用<html ngapp>以及角1.4.8

将其更改为1.2.8并消除了错误。

我的HTML了 <div class ="container" ng-controller="AppCtrl">

我有一个带有函数

的controller.js文件

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

答案 8 :(得分:0)

就我而言:

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

答案 9 :(得分:0)

要解决这个问题,我必须发现我在Angular路线声明中错误拼写了控制器的名称:

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

答案 10 :(得分:0)

<sript

而不是

<script 

;)同时链接controller.js。在我的情况下

相关问题