AngularJS + ASP.NET MVC - 控制器未定义 [英] AngularJS + ASP.NET MVC - Controller undefined
问题描述
喂大家,我有AngularJs不reconizing我的控制器有问题,我总结了我下面的完成。
起点:AngularJs教程 http://www.thinkster.io/angularjs/ZPCQtVfGba / angularjs控制器
问题:
在文件中定义的控制器(被正确检索时访问页),加入NG-控制器指令相关DIV,得到的JavaScript运行时错误(
错误:[NG:AREQ] <一个href=\"http://errors.angularjs.org/1.3.0-beta.18/ng/areq?p0=FirstCtrl&p1=not%20a%20function%2C%20got%20undefined\" rel=\"nofollow\">http://errors.angularjs.org/1.3.0-beta.18/ng/areq?p0=FirstCtrl&p1=not%20a%20function%2C%20got%20undefined
)
发布我检查我是否已经列入我的控制器实现,包括角之前,但它并非如此。之前
我在想什么?我怀疑这可能是与在全球范围内声明的控制器(例如<一个href=\"http://stackoverflow.com/questions/25111831/controller-not-a-function-got-undefined-while-defining-controllers-globally\">Controller不是一个函数,得到了不确定,同时定义控制器全球),但我真的不知道这是否是真实的情况。
提前感谢!
_layout .cshtml
&LT;!DOCTYPE HTML&GT;
&LT; HTML NG-应用&GT;
&LT; HEAD&GT;
&LT;间的charset =UTF-8/&GT;
&LT; META NAME =视口CONTENT =WIDTH =设备宽度,初始规模= 1.0&GT;
&LT;标题&GT; @ ViewBag.Title - 我的ASP.NET应用程序&LT; /标题&GT;
@RenderSection(PageSpecificStyles,必需:false)
&LT; /头&GT;
&LT;身体GT; @RenderBody()
@RenderSection(BodyScripts,必需:false)
&LT; /身体GT;
&LT; / HTML&GT;
Index.cshtml:
@ {
ViewBag.Title =主页;
}@section PageSpecificStyles {
&LT;风格&GT;
DIV {
保证金左:20%;
}
&LT; /风格&GT;
}@section BodyScripts {
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js\"></script>
&LT;脚本SRC =〜/脚本/ FirstCtrl.js&GT;&LT; / SCRIPT&GT;
}
&LT; DIV NG控制器=FirstCtrl&GT; &LT; DIV&GT;
&LT; H1&GT;您好{{data.message}}&LT; / H1&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
FirstCtrl.js(在〜/脚本)
函数FirstCtrl($范围){
$ scope.data = {消息:猫 };
}
当你怀疑,因为你使用的是1.3角测试版<一个href=\"http://stackoverflow.com/questions/25111831/controller-not-a-function-got-undefined-while-defining-controllers-globally\">which对全球控制器构造函数发现没有隐含的支持,需要与模块注册您的控制器。
angular.module('yourApp')控制器('FirstCtrl',['$范围,FirstCtrl]);
和也是自要定义模块,你需要指定你的NG-应用模块的名字。
&LT; HTML NG-应用=yourApp&GT;
Hullo everyone, I am having a problem with AngularJs not reconizing my controller, I have summarised what I have done below.
Starting point: AngularJs tutorial http://www.thinkster.io/angularjs/ZPCQtVfGba/angularjs-controllers
Problem: defined controller in file (gets retrieved correctly when visiting page), added ng-controller directive to relevant div, got javascript runtime error (
Error: [ng:areq] http://errors.angularjs.org/1.3.0-beta.18/ng/areq?p0=FirstCtrl&p1=not%20a%20function%2C%20got%20undefined
)
Before posting I checked whether I had included my controller implementation before including Angular but it is not the case.
What am I missing? I suspect it might have something to do with declaring controllers in the global scope (e.g. Controller not a function, got undefined, while defining controllers globally) but I am not really sure whether this is indeed the case.
Thanks in advance!
_Layout .cshtml
<!DOCTYPE html>
<html 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>
@RenderSection("PageSpecificStyles",required:false)
</head>
<body>
@RenderBody()
@RenderSection("BodyScripts", required: false)
</body>
</html>
Index.cshtml:
@{
ViewBag.Title = "Home Page";
}
@section PageSpecificStyles{
<style>
div {
margin-left: 20%;
}
</style>
}
@section BodyScripts{
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>
<script src="~/Scripts/FirstCtrl.js"></script>
}
<div ng-controller="FirstCtrl">
<div>
<h1>Hello {{data.message}}</h1>
</div>
</div>
FirstCtrl.js (in ~/scripts)
function FirstCtrl($scope) {
$scope.data = { message: "Cat!" };
}
As you suspected, since you are using angular 1.3 beta which has no implicit support for Global controller constructor function discovery, you need to register your controller with the module.
angular.module('yourApp').controller('FirstCtrl',['$scope', FirstCtrl]);
and also since you are defining an module you need to specify the module name in your ng-app.
<html ng-app="yourApp">
这篇关于AngularJS + ASP.NET MVC - 控制器未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!