1

我创建了一个 AngularJS 应用程序来显示我的 Tumblr 仪表板。我遇到的问题是浏览器中没有返回任何数据。但是,如果我刷新页面并在页面完成加载之前立即导航到不同的选项卡,则当我导航回原始选项卡时数据将在那里。有没有人遇到过这样的问题?任何想法我做错了什么?

应用程序.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>

4

1 回答 1

1

在异步函数(回调/承诺)中修改范围后,您必须使用 $scope.$apply() 来绑定视图中的新值:

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();
      });
    });
  });

看看这个工作示例:http: //jsfiddle.net/22spy726/2/

于 2015-06-01T13:34:02.927 回答