在 AngularJs 中,从工厂服务到控制器的返回值丢失

Return value from factory service to controller in AngularJs is lost

本文关键字:控制器 返回值 服务 AngularJs 工厂      更新时间:2023-09-26

我想将列表对象从工厂服务返回到控制器。当我从服务返回列表对象时,它包含数据,但是当控件到达调用控制器时,列表变为空(值丢失)。

我在文本输入上调用 ng-change="search()"

代码如下:

工厂服务.js

    app.factory('newService', ['$http', '$q','$filter', function ($http, $q, $filter) 
{
    var ServiceFactory = {};
   var searchMatch = function (haystack, needle) {
        if (!needle) {
            return true;
        }
        return haystack.toString().toLowerCase().indexOf(needle.toLowerCase()) !== -1;
    };
    var _search = function (Items, data, sortingOrder, query, reverse) {
        Items = $filter('filter')(data, function (item) {
            for (var attr in item) {
                if (angular.isUndefined(item[attr]) || item[attr] === null)
                    continue;
                if (searchMatch(item[attr], query))
                    return true;
            }
            return false;
        });
        // take care of the sorting order
        if (sortingOrder !== '') {
            Items = $filter('orderBy')(Items, sortingOrder, reverse);
        }
        return Items;
    };
    ServiceFactory.search = _search;
    return ServiceFactory;
}]);

在这里,搜索方法中的"返回项目"具有正确的值。

控制器.js

app.controller('mycontroller', ['$scope', '$http', '$filter', 'Service', function ($scope, $http, $filter, Service) {
    var sortingOrder = 'Site'; 
    $scope.sortingOrder = sortingOrder;
    $scope.reverse = false;
    $scope.Items = [];
    $scope.Data = function () {
        $scope.loading = true;
        $http({
            url: "http://localhost:50100/api/mycontroller",
            dataType: 'json',
            method: 'GET',
            data: '', // data:'', for input parameters, create a small entity class and assign it
            headers: {
                "Content-Type": "application/json"
                // "Content-Type": 'application/x-www-form-urlencoded'
            }
        }).success(function (response) {
            $scope.Data = response;
            $scope.Items = $scope.Data;
        })
       .error(function (error) {
           alert(error);
       })
         .finally(function () {
             $scope.loading = false;
         });
    }
    $scope.search = function () {
        return Service.search($scope.Items, $scope.Data, $scope.sortingOrder, $scope.query, $scope.reverse);
    };
}]);

在这里,$scope.search 不包含任何值(此处丢失数据)。

我是 angularJS 的新手,希望有人能在这方面帮助我。提前谢谢。

您需要将

服务名称(newService不是Service)注入控制器,例如:

app.controller('mycontroller', ['$scope', '$http', '$filter', 'newService', function ($scope, $http, $filter, newService) {

因此,您的$scope.search函数应修改为:

$scope.search = function () {
        return newService.search($scope.Items, $scope.Data, $scope.sortingOrder, $scope.query, $scope.reverse);
};

如果上述方法不起作用,我可以仔细查看您的服务设置。

您的$scope.Data函数正在成功处理程序函数中替换自身。

$scope.Data = function () {
        $scope.loading = true;
        $http({
            url: "http://localhost:50100/api/mycontroller",
            dataType: 'json',
            method: 'GET',
            data: '', // data:'', for input parameters, create a small entity class and assign it
            headers: {
                "Content-Type": "application/json"
                // "Content-Type": 'application/x-www-form-urlencoded'
            }
        }).success(function (response) {
            //THIS IS A PROBLEM
            $scope.Data = response;
            $scope.Items = $scope.Data;
        })
       .error(function (error) {
           alert(error);
       })
         .finally(function () {
             $scope.loading = false;
         });
    }

谢谢你的回复朋友...!!

我刚刚更改了以下代码行,它似乎正在工作。

$scope.search = 函数 () { $scope.search = Service.search($scope.项目,$scope。data, $scope.sortingOrder, $scope.query, $scope.reverse); };