Ich bin neu in angularjs. Mein Ziel ist sehr einfach. Ich möchte einen Ajax-Aufruf machen, um Daten zu erhalten, und wenn ich fertig bin, möchte ich einen zweiten Aufruf machen, um einen anderen Satz von Daten zu erhalten, der von Informationen in der ersten Menge abhängt.
Ich versuche, dies zu tun Verheißungsmechanismen verwenden, so dass ich Verkettung anstelle von verschachtelten Ajax-Aufrufe verwenden kann und die Fähigkeit, unabhängige Funktionen zu haben, die ich bei Bedarf verknüpfen kann.
Mein Code sieht ungefähr wie folgt:
var promiseGetWorkTypes = function ($q, $scope, $http) {
\t console.log("promiseGetWorkTypes");
\t return $q(function (resolve, reject) {
\t \t $http({
\t \t \t method: 'GET',
\t \t \t url: '/WorkTypes'
\t \t }).then(
\t \t \t function (payload) {
\t \t \t \t console.log("Got workttypegroups")
\t \t \t \t console.log(payload);
\t \t \t \t $scope.WorkTypeGroups = payload.data;
\t \t \t \t console.log("End of worktypegroups");
\t \t \t \t resolve(payload);
\t \t \t },
\t \t \t function (payload) {
\t \t \t \t reject(payload);
\t \t \t });
\t });
};
var promiseGetRecentActivities = function ($q, $scope, $http) {
\t console.log("promiseGetRecentActivities");
\t return $q(function (resolve, reject) {
\t \t $http({
\t \t \t method: 'GET',
\t \t \t url: '/RecentHistory'
\t \t }).then(
\t \t \t function (payload) {
\t \t \t \t $scope.RecentActivities = payload.data;
\t \t \t \t resolve(payload);
\t \t \t },
\t \t \t // data contains the response
\t \t \t // status is the HTTP status
\t \t \t // headers is the header getter function
\t \t \t // config is the object that was used to create the HTTP request
\t \t \t function (payload) {
\t \t \t \t reject(payload);
\t \t \t });
\t });
};
var index = angular.module("index", []);
index
.controller('EntitiesController', function ($scope, $http, $timeout, $q) {
\t promiseGetWorkTypes($q, $http, $scope)
\t \t .then(promiseGetRecentActivities($q, $http, $scope));
}
Allerdings, wenn ich an meine Debug-Konsole schauen, wie ich sehe, dass der Anruf auf "promiseGetRecentActivities" vor dem Aufruf beginnt die Ajax Handling ist bei "promiseGetWorkTypes" aufgetreten.
Was vermisse ich oder mache ich hier falsch?
Verwenden Sie '$ q.all' http://jsfiddle.net/ThomasBurleson/QqKuk/ – underscore
Nein, nicht' $ q.all() '. OP möchte die beiden Ajax Calls in Serie schalten. –
http://stackoverflow.com/questions/23803743/what-is-the-sefered-antipattern-and-how-do-i-avoid-it –