从AngularJS中的.factory获取数据

时间:2015-12-03 12:31:41

标签: html angularjs

我试图访问名为app.factory.js的.factory文件中的数据。那部分看起来像这样:

.factory('avengersService', function ($log) {
            return {
                refresh: function () {          
                    $log.info('Executed refresh()');            
                },
                getAvengers: function() {       
                console.log('getting data');            
                    var avengers = [
                        {                       
                            'Name': 'Iron Man',
                            'Real Name': 'Anthony "Tony" Edward Stark',
                            'Member Since': '1963',
                            'Notes': 'Founder of original roster. Joined West Coast Branch in West Coast Avengers vol. 2 #1 (1984). Expelled for Armor War (1987). Current member of the main Avengers team.'   
                        },
                        {
                            'Name': 'Thor',
                            'Real Name': 'Thor Odinson (a.k.a. Dr. Donald Blake, Sigurd Jarlson, Jake Olsen, Odinson)',
                            'Member Since': '1963',
                            'Notes': 'Former member of the main Avengers team.',    
                        },
                        {
                            'Name': 'Ant-Man',
                            'Real Name': 'Dr. Henry Jonathan "Hank" Pym',
                            'Member Since': '1963',
                            'Notes': 'Became Giant-Man in Avengers #2 (1963), Goliath in Avengers #28 (1966), and Yellowjacket in Avengers #59 (1969). Expelled as Yellowjacket in Avengers #213 (1981). Joined West Coast Branch as Doctor Pym in West Coast Avengers #21 (1988). Became The Wasp in Mighty Avengers #21 (March 2009). He is one of only two members, the other being Kelsey Leigh (Captain Britain), who have Avengers status in both their civilian and superhero guises. Formerly the head of the Avengers Academy, former member of the Secret Avengers and formerly the leader of the Avengers A.I. Squad. Currently merged with Ultron.',
                        }
                    ]
                    console.log('getting data DONE');                                       
                }
            }            
        })

在我的控制器文件app.controller.js中,我想检索该数据:

function AvengersController(avengersService, logger) {
            var vm = this;
            vm.avengers = [];
            vm.getAvengers = getAvengers;
            vm.title = 'Avengers';

            activate();

            function activate() {
                return getAvengers().then(function() {
                    logger.info('Activated Avengers View');
                });
            }

            function getAvengers() {
                return avengersService.getAvengers().then(function(data) {
                    vm.avengers = data;
                    return vm.avengers;
                });
            }
        }

这是控制台当前显示的内容:

enter image description here

现在有些提示会很棒!

3 个答案:

答案 0 :(得分:3)

将工厂的getAvengers功能更改为:

getAvengers: function() {

    return [
        {
            'Name': 'Iron Man',
            'Real Name': 'Anthony "Tony" Edward Stark',
            'Member Since': '1963',
            'Notes': 'Founder of original roster. Joined West Coast Branch in West Coast Avengers vol. 2 #1 (1984). Expelled for Armor War (1987). Current member of the main Avengers team.'
        },
        {
            'Name': 'Thor',
            'Real Name': 'Thor Odinson (a.k.a. Dr. Donald Blake, Sigurd Jarlson, Jake Olsen, Odinson)',
            'Member Since': '1963',
            'Notes': 'Former member of the main Avengers team.',
        },
        {
            'Name': 'Ant-Man',
            'Real Name': 'Dr. Henry Jonathan "Hank" Pym',
            'Member Since': '1963',
            'Notes': 'Became Giant-Man in Avengers #2 (1963), Goliath in Avengers #28 (1966), and Yellowjacket in Avengers #59 (1969). Expelled as Yellowjacket in Avengers #213 (1981). Joined West Coast Branch as Doctor Pym in West Coast Avengers #21 (1988). Became The Wasp in Mighty Avengers #21 (March 2009). He is one of only two members, the other being Kelsey Leigh (Captain Britain), who have Avengers status in both their civilian and superhero guises. Formerly the head of the Avengers Academy, former member of the Secret Avengers and formerly the leader of the Avengers A.I. Squad. Currently merged with Ultron.',
        }
    ]
}

你的控制器:

function AvengersController( avengersService, logger ) {
    var vm      = this;
    vm.avengers = [];
    vm.title    = 'Avengers';

    activate();

    function activate() {
        vm.avengers = avengersService.getAvengers();
    }
}

您的工厂没有任何回调,并且由于您只需要从工厂获取数据,所以您只需要这样做。

答案 1 :(得分:0)

这只是因为你没有从你的功能中返回任何东西。

你的函数getAvengers()在工厂中没有返回/回调语句。

这是应该有用的东西:

app.factory('MyService',['$http',function($http){
    return {
        refresh: function () {          
            $log.info('Executed refresh()');            
        },
        getAvengers: function(callback) {       
            console.log('getting data');            
                callback([
                    {                       
                        'Name': 'Iron Man',
                        'Real Name': 'Anthony "Tony" Edward Stark',
                        'Member Since': '1963',
                        'Notes': 'Founder of original roster. Joined West Coast Branch in West Coast Avengers vol. 2 #1 (1984). Expelled for Armor War (1987). Current member of the main Avengers team.'   
                    },
                    {
                        'Name': 'Thor',
                        'Real Name': 'Thor Odinson (a.k.a. Dr. Donald Blake, Sigurd Jarlson, Jake Olsen, Odinson)',
                        'Member Since': '1963',
                        'Notes': 'Former member of the main Avengers team.',    
                    },
                    {
                        'Name': 'Ant-Man',
                        'Real Name': 'Dr. Henry Jonathan "Hank" Pym',
                        'Member Since': '1963',
                        'Notes': 'Became Giant-Man in Avengers #2 (1963), Goliath in Avengers #28 (1966), and Yellowjacket in Avengers #59 (1969). Expelled as Yellowjacket in Avengers #213 (1981). Joined West Coast Branch as Doctor Pym in West Coast Avengers #21 (1988). Became The Wasp in Mighty Avengers #21 (March 2009). He is one of only two members, the other being Kelsey Leigh (Captain Britain), who have Avengers status in both their civilian and superhero guises. Formerly the head of the Avengers Academy, former member of the Secret Avengers and formerly the leader of the Avengers A.I. Squad. Currently merged with Ultron.',
                    }
                ]);                       
            }
        };
    }]);

这是一个小提琴:http://jsfiddle.net/csdyvwvv/

答案 2 :(得分:0)

这看起来很有趣,所以我创建了一个plunker,但似乎你已经找到了答案。

唯一不同的是,我让工厂发出了承诺。

angular.module('AvengersApp')
    .factory('AvengersFactory', ['$q', AvengersFactory]);

    function AvengersFactory($q) {
        var avengers = [
            {                       
                'Name': 'Iron Man',
                'Real Name': 'Anthony "Tony" Edward Stark',
                'Member Since': '1963',
                'Notes': 'Founder of original roster. Joined West Coast Branch in West Coast Avengers vol. 2 #1 (1984). Expelled for Armor War (1987). Current member of the main Avengers team.'   
            },
            {
                'Name': 'Thor',
                'Real Name': 'Thor Odinson (a.k.a. Dr. Donald Blake, Sigurd Jarlson, Jake Olsen, Odinson)',
                'Member Since': '1963',
                'Notes': 'Former member of the main Avengers team.',    
            },
            {
                'Name': 'Ant-Man',
                'Real Name': 'Dr. Henry Jonathan "Hank" Pym',
                'Member Since': '1963',
                'Notes': 'Became Giant-Man in Avengers #2 (1963), Goliath in Avengers #28 (1966), and Yellowjacket in Avengers #59 (1969). Expelled as Yellowjacket in Avengers #213 (1981). Joined West Coast Branch as Doctor Pym in West Coast Avengers #21 (1988). Became The Wasp in Mighty Avengers #21 (March 2009). He is one of only two members, the other being Kelsey Leigh (Captain Britain), who have Avengers status in both their civilian and superhero guises. Formerly the head of the Avengers Academy, former member of the Secret Avengers and formerly the leader of the Avengers A.I. Squad. Currently merged with Ultron.',
            }
        ];

        var factory = {
            getAvengers: getAvengers
        };

        function getAvengers() {
            return $q(function(resolve, reject) {
                resolve(avengers);
            });
        }

        return factory;
    }