错误:[ng:areq] http://errors.angularjs.org/1.3.8/ng/areq?p0=homeIndexController&p1=不是函数,未定义

时间:2014-12-26 19:05:50

标签: angularjs asp.net-mvc-4 visual-studio-2013 ninject bootswatch

请不要将此标记为重复。我已经看到堆栈溢出中发布了许多重复的错误。但我的情况是,我有最简单的应用程序发布,但得到相同的错误。

我正在努力学习Angular JS。下面是我的_Layout.chtml看起来像。

<!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>

我的Index.chtml如下:

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

我的home-index.js文件如下:

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

另外,我看到用data替换data-ng-app和data-ng-controller    ng-app和ng-controller 不像我的一些朋友建议的那样修复问题。

在控制台中,我看到打印了In home-index,但函数未定义。我检查了函数Name并与控制器声明匹配。

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

欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

我首先想到的不是回答。然后我认为像我这样的新手知道许多最简单的角度例子讲述在全局范围内使用全局控制器声明会很棒。有关此问题的技术细节可以在here找到。感谢PSL迅速将我指向正确的位置。

我提供了#34; app&#34;到ng-app

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

并更改了我的javascript调用,如下所示:

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