Mark Anderson
Mark Anderson

Reputation: 25

Tumblr Feed using AngularJS and OAuth.io

I created an AngularJS app that displays my Tumblr dashboard. The issue I have is that there is not any data being returned in the browser. However, if I refresh the page and immediately navigate to a different tab before the page completes loading, the data will be there when I navigate back to the original tab. Has anyone ever run into an issue like this before? Any ideas what I'm doing wrong?

app.js

'use strict';

/**
 * @ngdoc overview
 * @name instafeed
 * @description
 * # instafeed
 *
 * Main module of the application.
 */
angular
  .module('instafeed', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/main', {
        templateUrl: 'views/main.html',
        controller: 'ShowTumblr'
      });
  });

main.js

'use strict';

angular.module('instafeed')
.controller('ShowTumblr', function($scope){
	var endpoint = 'https://api.tumblr.com/v2/user/dashboard';
	OAuth.initialize('[oaut.io key]');
	OAuth.popup('tumblr', {cache: true}).done(function(result) {
		result.get(endpoint).done(function(data){
			$scope.posts = data.response.posts;
			console.log($scope.posts);
		});
	});
});

main.html

<div class="row" ng-controller="ShowTumblr">
  <div class="col-md-12" ng-repeat="x in posts">
    <a href="{{ x.image_permalink }}" target="_blank">
      <img src="{{ x.photos[0].alt_sizes[1].url }}" alt="">
    </a>
  </div>
</div>

Upvotes: 1

Views: 732

Answers (1)

Thibaud Arnault
Thibaud Arnault

Reputation: 1435

You have to use $scope.$apply() after modifying the scope in an async function (callbacks/promises) to bind the new value in the view:

angular.module('instafeed')
  .controller('ShowTumblr', function($scope){
    var endpoint = 'https://api.tumblr.com/v2/user/dashboard';
    OAuth.initialize('[oauth.io key]');
    OAuth.popup('tumblr', {cache: true}).done(function(result) {
      result.get(endpoint).done(function(data){
        $scope.posts = data.response.posts;
        $scope.$apply();
      });
    });
  });

Take a look at this working exemple: http://jsfiddle.net/22spy726/2/

Upvotes: 1

Related Questions