使用 AngularJS 和 OAuth.io 的 Tumblr Feed
Tumblr Feed using AngularJS and OAuth.io
我创建了一个 AngularJS 应用程序来显示我的 Tumblr 仪表板。我遇到的问题是浏览器中没有返回任何数据。但是,如果我刷新页面并在页面完成加载之前立即导航到另一个选项卡,则当我导航回原始选项卡时,数据将在那里。有没有人 运行 遇到过这样的问题?知道我做错了什么吗?
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>
您必须在异步函数 (callbacks/promises) 中修改作用域后使用 $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/
我创建了一个 AngularJS 应用程序来显示我的 Tumblr 仪表板。我遇到的问题是浏览器中没有返回任何数据。但是,如果我刷新页面并在页面完成加载之前立即导航到另一个选项卡,则当我导航回原始选项卡时,数据将在那里。有没有人 运行 遇到过这样的问题?知道我做错了什么吗?
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>
您必须在异步函数 (callbacks/promises) 中修改作用域后使用 $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/