AngularJS 使用工厂方法创建服务时未定义模块

AngularJS Module is undefined while creating service using Factory method

我正在学习 AngularMVC 中的 JS,并创建了将我的模型传递给 Angular 控制器的服务。但它给了我错误

JavaScript runtime error: '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 开始,然后是相关包(外部 CSS,JS)。

2.In 为了传递模型数据,我们需要在 .cshtml 文件中有一个内联元素(尽管不推荐)但是,如果这是要走的路,请使用服务注入到相关控制器-

@section scripts
    {
    <script>
    (function (undefined) {
        window.app.factory('searchBinding', function () {
            return @Html.Raw(Json.Encode(Model))
        })
    })(undefined);

</script>
}
  1. 现在,由于上面的代码是内联的,它与 View 本身一起执行,但是 Angular 模块尚未定义为它待下载。因此它会抛出 undefined 错误。

  2. 为了解决这个问题,我们可以在 _Layout.cshtml 中使用 RenderSection 在加载所有核心文件(包)之后,这甚至会强制内联部分仅在加载这些文件后加载(在步骤 2 中。@section 脚本用于此目的)。_Layout.cshtml 文件

    中的片段

    @Scripts.Render("~/bundles/angularjs"); @RenderSection("scripts",false);