Derzeit habe ich Probleme mit dem Warten auf meine Versprechen zu lösen. Ich verstehe, dass es weil es ein asynchroner Anruf ist, so dass die Lösung nicht auf alle Versprechungen wartet und nur einen Teil meiner Daten zur Lösung übergibt.Angularjs UI-Router Lösung wartet nicht auf alle Versprechen
Ich habe versucht, viele Foren zu suchen, aber ich kann nicht scheinen, es zur Arbeit zu bekommen.
so hier ist meine Homepage.
angular.module('app')
.component('home', {
templateUrl: 'Content/app/components/home.html',
bindings: {},
controller: ['$http', '$state', 'test',
function ($http, $state, test) {
var vm = this;
vm.userName;
vm.searchReddit = function() {
$http.get('https://www.reddit.com/user/' + vm.userName + '/about.json')
.then(function (response) {
vm.accountData = response.data.data;
vm.accountData.total_karma = vm.accountData.comment_karma + vm.accountData.link_karma;
$state.go('redditAnalyzer', { name: vm.userName });
});
}
}
]
});
Sobald ich meinen Benutzernamen eintippe, ändert sich der Status. und in meinem app.js ich habe
angular.module('app', ['ui.router', 'chart.js'])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('redditAnalyzer', {
url: '/analyze/:name',
component: 'redditAnalyzer',
resolve: {
resolve:
['test', '$stateParams', function (test, $stateParams) {
var data = test.getAnalysisData($stateParams.name);
return data;
}
]
}
})
.state('home', {
url: '/',
component: 'home'
});
}]);
ich die Funktion test.getAnalysisData bin ruft die Daten zu erhalten.
und in meinem test.js Ich habe
angular.module('app')
.factory('test', ['$http', '$state', '$q', function ($http, $state, $q) {
var vm = this;
vm.accountData = {};
vm.after = '';
vm.bestComment = {};
vm.bestComment.karma = 0;
vm.userName;
vm.bestComment.date = '';
vm.subreddit = {};
vm.myChart;
vm.getAnalysisData = function (redditUser) {
vm.userName = redditUser;
vm.resetData(redditUser);
vm.getAccountInfo(redditUser);
vm.getAllComments(redditUser);
return {
accountData: vm.accountData,
bestComment: vm.bestComment,
userName: vm.userName,
subreddit: vm.subreddit,
myChart: vm.myChart
};
}
vm.resetData = function (user) {
vm.accountData = {};
vm.after = '';
vm.bestComment = {};
vm.bestComment.karma = -(Math.pow(2, 53) - 1);
vm.userName = user;
vm.date = '';
vm.subreddit = [];
vm.topThreeSub = [];
}
vm.getAccountInfo = function (user) {
$http.get('https://www.reddit.com/user/' + user + '/about.json')
.then(function (response) {
vm.accountData = response.data.data;
vm.accountData.total_karma = vm.accountData.comment_karma + vm.accountData.link_karma;
console.log("I got the account info!");
});
}
vm.getAllComments = function (user) {
$http.get('https://www.reddit.com/user/' + user + '/comments.json' + vm.after)
.then(
function (response) {
console.log("I got the comment info!");
tempResponse = response;
var data = response.data.data
vm.after = '?after=' + data.after;
for (i = 0; i < data.children.length; i++) {
if (vm.bestComment.karma < parseInt(data.children[i].data.score)) {
vm.bestComment.karma = parseInt(data.children[i].data.score);
vm.bestComment.comment = data.children[i].data.body;
vm.bestComment.date = (new Date(data.children[i].data.created * 1000)).toString();
}
var tempSub = data.children[i].data.subreddit;
if (vm.subreddit[tempSub] === undefined) {
vm.subreddit[tempSub] = 1;
}
else {
vm.subreddit[tempSub]++;
}
}
if (response.data.data.after != null) {
vm.getAllComments(user);
}
}, function (error) {
console.log(error);
throw error;
})
.catch(function (error) { });
}
return {
resolve: vm.resolve,
hello: vm.hello,
getAnalysisData: vm.getAnalysisData
}
}]);
ich die Funktion vm.getAllComments rekursiv ich rufe an, weil, wie es funktioniert vm.getAllComments ist ersten 25 Kommentare des Kontos erhalten wird, und dann von einem Teil der Info aus der Antwort kann ich die nächsten 25 Kommentare in das Konto bekommen.
und schließlich in meiner redditanalyzer Datei, ich habe
angular.module('app')
.component('redditAnalyzer', {
templateUrl: 'Content/app/components/redditAnalyzer.html',
bindings: {
resolve: '<'
},
controller: ['$http', 'test',
function ($http, test) {
var vm = this;
vm.accountData = {};
vm.bestComment = {};
vm.bestComment.karma = 0;
vm.userName;
vm.bestComment.date = '';
vm.subreddit = {};
vm.myChart;
vm.$onInit = function() {
console.log("this is the resolve", vm.resolve);
//vm.accountData = resolve.accountData;
//vm.bestComment = resolve.bestComment;
//vm.myChart = resolve.myChart;
//vm.subreddit = resolve.subreddit;
//vm.userName = resolve.userName;
}
}]
});
Und Sie können das Problem in meiner Konsole.
this is the resolve {accountData: {…}, bestComment: {…}, userName: "spez", subreddit: Array(0), myChart: undefined}accountData: {}bestComment: {karma: 22200, comment: "Reddit search might work by then.", date: "Thu Oct 27 2016 01:07:22 GMT-0400 (Eastern Daylight Time)"}myChart: undefinedsubreddit: [blog: 10, announcements: 475, modnews: 34, programming: 32, ModSupport: 20, …]userName: "spez"__proto__: Object
test.js:44 I got the account info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
test.js:51 I got the comment info!
7test.js:51 I got the comment info!
Es ändert die Route, noch bevor die Versprechungen gemacht werden. Was soll ich tun, um auf all meine Versprechen zu warten?
Der Resolver wartet nicht auf das Versprechen lesen, weil der Code nicht zurückgibt ein Versprechen an den Resolver. – georgeawg