Error: [ng:areq] http://errors.angularjs.org/1.3.8/ng/areq?p0=homeIndexController&p1=not a function, got undefined [duplicate]

巧了我就是萌 提交于 2019-12-12 04:45:18

问题


Please do not mark this as a duplicate. I have seen many duplicates of this error posted in stack overflow. But my case is, I have the simplest of the applications posted there, but getting the same error.

I am trying to learn Angular JS. Below is my _Layout.chtml looks like.

<!DOCTYPE html>
<html lang="en" data-ng-app="">
<head>
    <meta charset="utf-8">
    <!-- Title here -->
    <title>@ViewBag.Title - MessageBoard</title>
    <!-- Description, Keywords and Author -->
    <meta name="description" content="Your description">
    <meta name="keywords" content="Your,Keywords">
    <meta name="author" content="ResponsiveWebInc">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Stylesheets -->
    <link href="~/Content/Template/css/bootstrap.min.css" rel="stylesheet">
    <link href="~/Content/Template/css/ddlevelsmenu-base.css" rel="stylesheet">
    <link href="~/Content/Template/css/ddlevelsmenu-topbar.css" rel="stylesheet">
    <link href="~/Content/Template/css/flexslider.css" rel="stylesheet">
    <link href="~/Content/Template/css/prettyPhoto.css" rel="stylesheet">
    <link href="~/Content/Template/css/font-awesome.min.css" rel="stylesheet">
    <link href="~/Content/Template/css/blue.css" rel="stylesheet">
    <link href="~/Content/Template/css/style.css" rel="stylesheet">
    <link href="~/Content/Site.css" rel="stylesheet">
    <!-- Favicon -->
    <link rel="shortcut icon" href="~/Content/Template/img/favicon/favicon.png">
</head>

<body>
    <div class="main">
        <div class="container">
            <div class="row">
                @RenderBody()
            </div>
        </div>
    </div>
    <script src="~/Scripts/jquery-2.1.1.min.js"></script>
    <script src="~/Scripts/angular.min.js"></script>    
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script src="~/Scripts/easing.js"></script>
    <script src="~/Scripts/ddlevelsmenu.js"></script>
    <script src="~/Scripts/flexslider.js"></script>
    <script src="~/Scripts/jquery.prettyPhoto.js"></script>
    <script src="~/Scripts/respond.min.js"></script>
    <script src="~/Scripts/html5shiv.js"></script>
    <script src="~/Scripts/custom.js"></script>    
    @RenderSection("PageScripts", required: false)
</body>
</html>

My Index.chtml is as below:

@{
    ViewBag.Title = "Home Page";
}
@section PageScripts
{
    <script src="~/js/home-index.js"></script>
}
<div data-ng-controller="homeIndexController">
    <div>Test</div>
</div>

My home-index.js file is as below:

//home-index.js
if (window.console) { console.log("In home-index"); }
function homeIndexController() {
    alert("Inside the home index controller");
}

Also, I see that replacing data-ng-app and data-ng-controller with ng-app and ng-controller do not fix the problem as some of my friends suggested.

In the Console, I see the In home-index is printed, but the function is undefined. I checked the function Name and matched with the controller declaration.

HTML1300: Navigation occurred.
File: localhost:50272
In home-index
Error: [ng:areq] http://errors.angularjs.org/1.3.8/ng/areq?p0=homeIndexController&p1=not%20a%20function%2C%20got%20undefined
   at Qb (http://localhost:50272/Scripts/angular.min.js:19:411)
   at sb (http://localhost:50272/Scripts/angular.min.js:20:1)
   at Anonymous function (http://localhost:50272/Scripts/angular.min.js:76:95)
   at Anonymous function (http://localhost:50272/Scripts/angular.min.js:57:255)
   at s (http://localhost:50272/Scripts/angular.min.js:7:406)
   at v (http://localhost:50272/Scripts/angular.min.js:57:124)
   at g (http://localhost:50272/Scripts/angular.min.js:52:9)
   at g (http://localhost:50272/Scripts/angular.min.js:52:26)
   at g (http://localhost:50272/Scripts/angular.min.js:52:26)
   at g (http://localhost:50272/Scripts/angular.min.js:52:26)
SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited.
File: localhost:50272

Any suggestions are welcome.


回答1:


I thought first not to post an answer. Then I thought it would be great for newbies like me to know that many of the simplest angular examples tell about using global controller declaration on the global scope. The technical details about this can be found here. Appreciate PSL's effort to quickly pointing me to the right place.

I provided "app" to ng-app

<html lang="en" data-ng-app="app">

and changed my javascript call as below:

angular.module('app', []).controller('homeIndexController', function homeIndexController($scope) {
    alert("Inside the home index controller");
});


来源:https://stackoverflow.com/questions/27660907/error-ngareq-http-errors-angularjs-org-1-3-8-ng-areqp0-homeindexcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!