在使用工厂方法创建服务时,AngularJS模块是未定义的
AngularJS Module is undefined while creating service using Factory method
我在MVC中学习AngularJS,并创建了服务来将我的模型传递给Angular控制器。但是它给了我错误
JavaScript运行时错误:'accountModule' is undefined
这就是我正在做的:
AccountController.cs
public ViewResult Index()
{
var accounts = new List<Accounts>
{
new Accounts
{
Id = 111,
Designation = "Tech Lead",
Name = "AAA"
},
new Accounts
{
Id = 222,
Designation = "Softwre Engineer",
Name = "BBB"
},
new Accounts
{
Id = 111,
Designation = "Project Manager",
Name = "CCC"
}
};
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var accountJson = JsonConvert.SerializeObject(accounts, Formatting.None, settings);
return this.View("Index", (object)accountJson);
}
Index.cshtml
<script type="text/javascript" src="../../Scripts/Modules/Account/Account.js"></script>
<div ng-app="accountModule">
<div ng-controller="accountController">
<h1>{{message}}</h1>
<div class="container" ng-init="employees in {{employees}}">
<h2>Employee Details</h2>
<div class="row">
<table class="table table-condensed table-hover">
<tr>
<td>
ID
</td>
<td>
Name
</td>
<td>
Designation
</td>
</tr>
<tr ng-repeat="emp in employees">
<td>
{{emp.id}}
</td>
<td>
{{emp.name}}
</td>
<td>
{{emp.designation}}
</td>
</tr>
</table>
</div>
</div>
<div>
@*{{emps}}*@
</div>
</div>
</div>
<script type="text/javascript">
accountModule.factory('accountService', function() {
var factory = {};
factory.employees = @Html.Raw(Model);
return factory;
});
</script>
Account.js
var account = angular.module('accountModule',[]);
account.controller('accountController', [
'$scope', 'accountService', function ($scope, accountService) {
$scope.employees = accountService.employees;
$scope.message = "Hi on " + (new Date()).toDateString();
}
]);
我不明白我哪里错了。
您需要将内联脚本移到account.js后面。
变量应该是account
而不是accountModule
<script src="angular.js"></script>
<script src="app.js"></script>
<script src="account.js"></script>
<script type="text/javascript">
//account.factory('accountService', function() { //this is alternative use below one
angular.module('accountModule').factory('accountService', function() {
var factory = {};
factory.employees = @Html.Raw(Model);
return factory;
});
</script>
MVC视图呈现顺序-1. 从View开始,然后是相关的bundle(外部CSS,JS)。
2。为了传递模型数据,我们需要在.cshtml文件中有一个内联元素(虽然不推荐),但是,如果这是一种方式,那么使用服务注入到相关的控制器-
@section scripts
{
<script>
(function (undefined) {
window.app.factory('searchBinding', function () {
return @Html.Raw(Json.Encode(Model))
})
})(undefined);
</script>
}
- 现在,由于上面的代码是内联的,它会与View本身一起执行,但是Angular模块还没有定义,因为它还没有被下载。因此,它将抛出undefined错误。
为了解决这个问题,我们可以使用RenderSection在_Layout。在加载了所有的核心文件(bundle)之后,这就强制即使是内联部分也只能在这些文件加载之后才加载(在步骤2中)。@section scripts用于此目的)。_Layout中的片段。cshtml文件
@Scripts.Render("~/包/angularjs");@RenderSection("脚本",假);
相关文章:
- 我可以像其他库一样将JointJS作为AngularJS模块注入吗
- 如何组合angularjs模块
- angularjs模块不工作
- AngularJs-模块已定义,但仍然得到“;不可用”;错误
- 检查AngularJS模块是否已启动
- 无法在coffeescript中加载angularjs模块
- 无法弄清楚为什么 angularJS 模块无法加载
- AngularJS 模块注入
- 继续得到 AngularJS 模块不可用 (ASP.NET 5)
- 列出 AngularJS 模块中声明的指令/控制器
- 如何在脚本标记的源属性中使用 AngularJS 模块常量
- AngularJS 模块与 CommonJS/ECMA6 模块
- JavaScript 和 AngularJs 模块的区别和用法
- 如何卸载 angularjs 模块
- 创建 angularjs 模块
- Angularjs 模块未正确加载
- 使用Grunt管理AngularJS模块的版本和发布
- 如何将angularJS模块加载到应用程序中
- AngularJS模块中的多重配置
- AngularJS模块应该在自己的文件中吗