1
angular.module('myApp.services', ['ngResource']).
  factory('Imgur', function($resource, $http) {

    $http.defaults.useXDomain = true;
    $http.defaults.headers.common['Authorization'] = 'Client-ID 123123123123';

    var albumsService = {};

    var albums = $resource('https://api.imgur.com/3/account/me123/albums').get();

    //I can access albums.data in ng-repeate in view, but not here?
    //returns undefined
    console.log(albums.data);

    albumsService.albums = function() {
      return albums;
    };

    return albumsService;
  });

使用这个作品

  <ul class="nav nav-pills">
    <li ng-repeat="album in imgur.albums().data">
      {{album.title}}
    </li>
  </ul>
4

1 回答 1

3

为了albums在您的服务中使用信息,请将回调传递给get()

var albums = $resource('https://api.imgur.com/3/account/me123/albums').get(function () {
    // Log out all the titles.
    albums.data.forEach(function (album) {
        console.log(album.title);
    });
});
于 2013-05-03T15:10:26.910 回答