简体   繁体   English

Angular.js找不到控制器

[英]Angularjs can't find the controller

I am so new in Angularjs .I create a mvc project in VS2015.In the layout page i added these files: 我是Angularjs的新手,我在VS2015中创建了一个mvc项目。在布局页面中,我添加了以下文件:

<html data-ng-app="">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/angular.min.js"></script>
</head>
<body>

        @RenderBody()


</body>
</html>

I added a java file called home-Index.js to js folder : 我在js文件夹中添加了一个名为home-Index.js的Java文件:

//home-index.js
function homeIndexController() {
    alert("hello word");
}

I have another view called index and here you can see the content 我有另一个称为index视图,在这里您可以看到内容

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

I am expecting to see the alert hello world but nothing happens.Why ? 我希望看到警报hello world但是什么也没发生。为什么?

If you passed version 1.3 you have to define your module as Ozrix stated in the comments. 如果通过版本1.3,则必须将模块定义为注释中所述的Ozrix。 In addition, you also have to attach your controller to it: 另外,您还必须将控制器连接到它:

var myApp = angular.module('myApp',[]); 
myApp.controller('homeIndexController', homeIndexController);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM