繁体   English   中英

AngularJS:如何从JSON文件中获取特定ID的数据

[英]AngularJS: How to fetch data of specific id from JSON file

在我的控制器类中,我从URL提取特定用户的ID,然后将其发送到Service OrderService现在在服务中,我想从JSON文件中检索此ID的数据,如何实现呢?

OrderCtrl

'use strict';
angular.module('Orders').controller('OrderCtrl', ['$scope', '$state', "SettingService", "OrderService","$stateParams", function($scope, $state, SettingService, OrderService,$stateParams) {


 var OrderId = $stateParams.orderId;

 $scope.orders = [];

  OrderService.getOrderDetails(OrderId).then(function(response){
    $scope.orders = response.data.data;
  }, function(error){

  })

}]);

OrderService.js

angular.module('Orders')
    .service('OrderService', ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService",
     function($http, $state, $resource, $q, SettingService, $localStorage, MessageService) {
        var service = {
            getOrderDetails : function(OrderId){
            Here I want to retrieve data from JSON file

    });
            }

        }

        return service;
    }]);

尝试使用这样的东西

'use strict';
angular.module('Orders').controller('OrderCtrl', ['$scope', '$state', "SettingService", "OrderService", "$stateParams", function ($scope, $state, SettingService, OrderService, $stateParams) {

    var OrderId = $stateParams.orderId;
    $scope.orders = [];

    OrderService.getOrderDetails(OrderId).then(function (response) {
        $scope.orders = response.data.data;
    });

}]);

// I act a repository for the remote json collection.
angular.module('Orders').service("OrderService", ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService",
        function ($http, $state, $resource, $q, SettingService, $localStorage, MessageService, handleResponse) {
            // Return public API.
            return ({
                getOrderDetails: getOrderDetails
            });
            // I get all the remote collection.
            function getOrderDetails(OrderId) {
                var request = $http({
                    method: "get",
                    url: '/ajax/order/details', // for example
                    params: {'id': OrderId}
                });
                return (request.then(handleResponse.success, handleResponse.error));
            }
        }]);

angular.module('Orders').service('handleResponse', function ($http, $q, $location) {
    return {
        error: function (response) {
            // The API response from the server should be returned in a
            // nomralized format. However, if the request was not handled by the
            // server (or what not handles properly - ex. server error), then we
            // may have to normalize it on our end, as best we can.
            if (!angular.isObject(response.data) || !response.data.message) {
                // Something was wrong, will try to reload
                return ($q.reject("An unknown error occurred."));
            }
            // Otherwise, use expected error message.
            return ($q.reject(response.data.message));
        },
        success: function (response) {
            return (response.data);
        }
    };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM