AngularJS控制器中的奇怪行为

时间:2014-07-23 23:13:05

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat infinite-scroll

我试图在控制器/工厂内访问我的资源响应,但我一直收到这个愚蠢的错误消息!

这是我的工厂:

angularApp.factory('Pokemons', function($resource, $cacheFactory){

    var cache    = $cacheFactory('pokemonsList'),
        Pokemons = {};


    function loadPokemons() {
        var q = $resource('http://pokeapi.co/api/v1/pokedex/',
                {callback: 'JSON_CALLBACK', alt: 'json'},
                { get: {method:'JSONP'} }).get();

        return q;
    }

    Pokemons.getPokemons = function() {
        if ( !cache.get('pokemons') ) {
            var data = loadPokemons();
            cache.put('pokemons', data);
            return data;
        } else {
            return cache.get('pokemons');
        };
    }

    return Pokemons;
});

我的控制器:

angularApp.controller('PokemonsCtrl', function($scope, Pokemons) {
    $scope.pokemons = Pokemons.getPokemons();

    $scope.loadMore = function() {
        console.log("hello");
        var last = $scope.pokemons.objects[0].pokemon[$scope.pokemons.objects[0].pokemon.length - 1];
        for(var i = 1; i <= 8; i++) {
          $scope.pokemons.objects[0].pokemon.push(last + i);
        }
    };
});

我想要的回复是$scope.pokemons.objects[0].pokemon

我一直在接受:无法阅读财产&#39; 0&#39;未定义 - 但如果我尝试在视图中使用ngRepeat循环相同的东西,那就没关系了。 我试图让无限滚动工作,请帮忙!

0 个答案:

没有答案