AngularJS服务无法正常工作。我在这里做错了什么?

时间:2018-12-13 18:38:38

标签: angularjs angularjs-scope angularjs-service

我的模块:

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在所有控制器之间传递数据,这似乎是最好的方法,除非我无法使其正常工作。

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

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

我只是使用完全相同的代码从头开始。我不知道我在旧代码中有什么,但我不在乎。这次工作正常。