【问题标题】:AngularJS service not working. What am I doing wrong here?AngularJS 服务不工作。我在这里做错了什么?
【发布时间】:2019-05-15 01:03:43
【问题描述】:

我的模块:

angular.module("aps",
[
    "ngAnimate", "ngTouch", "ui.grid", "ui.grid.saveState", "ui.grid.selection", "ui.grid.cellNav",
    "ui.grid.resizeColumns", "ui.grid.moveColumns", "ui.grid.pinning", "ui.bootstrap", "ui.grid.autoResize"
]);

我的服务:

angular.module("aps")
.service("OrderGridOptions",
    function() {
            return {
                enableFiltering: true,
                flatEntityAccess: true,
                showGridFooter: true,
                fastWatch: true,
                rowTemplate:
                    '<div ng-click="grid.appScope.selectOrder(row)" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" class="ui-grid-cell" ng-class="col.colIndex()" ui-grid-cell></div>',
                columnDefs:
                [
                    { name: "WorkOrderID", width: 100, displayName: "Order ID" },
                    { name: "CustomerID", width: 120, displayName: "Customer ID" },
                    { name: "POSID", width: 100 },
                    { name: "CreateDate", width: 120 },
                    { name: "CreatedBy", width: 120 },
                    { name: "Description", visible: false },
                    { name: "EstimatedFinishDate", visible: false },
                    { name: "EstimatedHours", visible: false },
                    { name: "EstimatedCostLabor", visible: false },
                    { name: "EstimatedCostParts", visible: false },
                    { name: "ActualFinishDate", width: 150 },
                    { name: "ActualCostLabor", visible: false },
                    { name: "ActualCostParts", visible: false },
                    { name: "CheckoutBy", width: 120 },
                    { name: "CheckoutNotes", visible: false }
                ],
                data: []
            };
    });

我的控制器:

angular.module("aps").controller("TechSheetCtl",
[
    "TechSheetFactory", "$scope", "$rootScope", "$http", "$interval", "$modal", "$log", "uiGridConstants", "$q",
    "$filter", "CustomerGridOptions", "OrderGridOptions",
    function(TechSheetFactory,
        $scope,
        $rootScope,
        $http,
        $interval,
        $modal,
        $log,
        uiGridConstants,
        $q,
        $filter,
        CustomerGridOptions,
        OrderGridOptions) {

$scope.testTheService = function() {
    alert("testing service");
    alert(JSON.stringify(OrderGridOptions));
}

我将 testTheService 附加到一个按钮上。两个警报都会出现,但第二个是空的。我试图不使用 $rootScope 在我的所有控制器之间传递数据,这似乎是最好的方法,但我无法让它工作。

而且我什至不知道我关于传递数据的假设是否正确。如果我在一个控制器中修改服务的数据数组,另一个控制器是否能够访问该数组?

我在这里错过了什么?

【问题讨论】:

  • 在代码中看不到任何错误,它应该可以工作。
  • 第二个警报语句缺少一个结束香蕉。服务实例是单例的。其内容的任何更改都会被所有人看到。
  • 最常见的问题之一是内联数组注释不匹配。构造函数的参数通常与字符串列表不匹配。我个人避免使用内联数组注释,因为它乏味且容易出错。最小化代码时可以使用ng-annotate 等自动化工具。
  • georgeawg:修复了参数。这不是问题。我只是粘贴了不完整的代码。
  • 我应该删除注释吗?我的意思是,我知道这不是问题,但是......

标签: angularjs angularjs-scope angularjs-service


【解决方案1】:

我只是使用完全相同的代码从头开始。我不知道旧代码中有什么,但我不在乎。这次它奏效了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2016-03-03
    • 2013-06-28
    • 2023-01-19
    • 2017-09-29
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多