如何在AngularJS Factory中按值获取单个对象

时间:2015-12-03 09:08:54

标签: angularjs json factory http-get

如何从航班数组中获取单个对象?我首先在一个视图中调用getFlights()函数,然后我想在转到特定航班的页面时在另一个视图中使用getFlight(flightId)。

但是,当我调用getFlight(flightId)时,它不包含任何数据。

angular.module('myApp.factories', []).
    factory('flightFactory', function ($http) {

        var flights = {};

        return {
            getFlights: function () {
                return $http.get('app/data/flights.json').then(function (response) {
                    flights = response;
                    return flights;
                });
            },
            getFlight: function (id) {

                for (var i = 0; i < flights.length; i++) {
                    if (flights[i].flightId == id) {
                        return flights[i];
                    }
                }
                return null;
            }
        };

    });

以下是我如何获得第一个控制器中的所有航班

flightFactory.getFlights().then(function(response) {
              $scope.flights = response.data;
});

以下是我在第二个控制器中试图获得单次飞行的方法

$scope.singleflight = flightFactory.getFlight($routeParams.flightID);

以下是JSON文件的内容

[
{
    "date": "2016-02-18T00:19:11-08:00",
    "numberOfSeats": 30,
    "totalPrice": 610,
    "flightId": "PR6674",
    "travelTime": 422,
    "destination": "South Sudan",
    "origin": "Copenhagen"
},
{
    "date": "2016-11-07T16:35:45-08:00",
    "numberOfSeats": 87,
    "totalPrice": 405,
    "flightId": "ZN4890",
    "travelTime": 196,
    "destination": "Rwanda",
    "origin": "Copenhagen"
},
{
    "date": "2016-07-11T18:05:32-07:00",
    "numberOfSeats": 36,
    "totalPrice": 646,
    "flightId": "AT1096",
    "travelTime": 399,
    "destination": "Equatorial Guinea",
    "origin": "Copenhagen"
}
]

非常感谢帮助!

2 个答案:

答案 0 :(得分:1)

@Jeffery Yu的作品,但它暴露了航班阵列。

angular.module('myApp.factories', []).
    factory('flightFactory', function ($http) {

        var flights = {};
        var _this = this;
        return {
            getFlights: function () {
                return $http.get('app/data/flights.json').then(function (response) {
                    _this.flights = response;
                    return _this.flights;
                });
            },
            getFlight: function (id) {
                var flights = _this.flights;
                for (var i = 0; i < flights.length; i++) {
                    if (flights[i].flightId == id) {
                        return flights[i];
                    }
                }
                return null;
            }
        };

    });

这将隐藏用户的航班数组(无法直接访问)。这就是@Anik Islam Abhi所说的。

答案 1 :(得分:0)

angular.module('myApp.factories', []).factory('flightFactory', function ($http) {

    return {
        flights:{},
        getFlights: function () {
            return $http.get('app/data/flights.json').then(function (response) {
                this.flights = response;
                return this.flights;
            });
        },
        getFlight: function (id) {

            for (var i = 0; i < flights.length; i++) {
                if (this.flights[i].flightId == id) {
                    return this.flights[i];
                }
            }
            return null;
        }
    };

});