在其他控制器中的另一个 $http 调用之后执行 $http 调用
Do $http call after another $http call in other controller
如何在 Match
控制器中的 $http.get
完成后调用 Portraits
控制器中的 $http.get
?我需要 idMatch
来给其他人打电话。
var app = angular.module('Rugby', ['ui.bootstrap']);
var idMatch = 0;
app.controller('Match', ['$http', '$log', function ($http, $log) {
var $scope = this;
$http.get('./lib/data.php?query=getMatches').success(function (response) {
var lastMatch = response.length - 1;
idMatch = response[lastMatch].idMatch;
$scope.date = response[lastMatch].date;
$scope.local = response[lastMatch].local;
$scope.visitor = response[lastMatch].visitor;
});
}]);
app.controller('Portraits', ['$http', '$log', function ($http, $log) {
var $scope = this;
this.players = [];
//I need to have the idMatch before this two calls starts
$http.get('./lib/data.php?query=getPlayersInMatch&idMatch=' + idMatch).success(function (response) {
$scope.players = response;
});
$http.get('./lib/data.php?query=getPlayersOutMatch&idMatch=' + idMatch).success(function (response) {
$scope.notConfirmedPlayers = response;
});
}]);
我现在在这上面浪费了两天,它可能很简单......
更新: 我的应用程序只是从所有比赛中检索数据,获取最后一场比赛的数据并显示参加比赛的球队和日期。然后它应该检索我们团队中所有将要玩它的球员并显示他们的照片和名字。
在不知道您的应用程序要求的情况下,我们只能假设您有正当理由将这两个控制器分开。
如果控制器是兄弟姐妹并且没有范围继承关系,您唯一的选择是通过$rootScope 广播一个事件。然而,这是不好的做法,应该避免。
如果它们有继承关系,child 将使用 $emit(向上)而不是 $broadcast
请阅读
https://docs.angularjs.org/guide/scope
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
了解范围继承。
以下示例(不是我的)http://jsfiddle.net/VxafF/,是一个简单易懂的示例。
$http.get('./lib/data.php?query=getMatches').success(function (response) {
var lastMatch = response.length - 1;
idMatch = response[lastMatch].idMatch;
$scope.date = response[lastMatch].date;
$scope.local = response[lastMatch].local;
$scope.visitor = response[lastMatch].visitor;
$rootScope.broadcast('matchIDEvent', {'idMatch': idMatch});
});
您的 Portraits Controller 然后需要一个新功能:
$rootScope.$on("myEvent",function (event, data) {
$http.get('./lib/data.php?query=getPlayersInMatch&idMatch=' + data.idMatch).success(function (response) {
$scope.players = response;
});
$http.get('./lib/data.php?query=getPlayersOutMatch&idMatch=' + data.idMatch).success(function (response) {
$scope.notConfirmedPlayers = response;
});
});
两个控制器还需要注入 rootScope。
如何在 Match
控制器中的 $http.get
完成后调用 Portraits
控制器中的 $http.get
?我需要 idMatch
来给其他人打电话。
var app = angular.module('Rugby', ['ui.bootstrap']);
var idMatch = 0;
app.controller('Match', ['$http', '$log', function ($http, $log) {
var $scope = this;
$http.get('./lib/data.php?query=getMatches').success(function (response) {
var lastMatch = response.length - 1;
idMatch = response[lastMatch].idMatch;
$scope.date = response[lastMatch].date;
$scope.local = response[lastMatch].local;
$scope.visitor = response[lastMatch].visitor;
});
}]);
app.controller('Portraits', ['$http', '$log', function ($http, $log) {
var $scope = this;
this.players = [];
//I need to have the idMatch before this two calls starts
$http.get('./lib/data.php?query=getPlayersInMatch&idMatch=' + idMatch).success(function (response) {
$scope.players = response;
});
$http.get('./lib/data.php?query=getPlayersOutMatch&idMatch=' + idMatch).success(function (response) {
$scope.notConfirmedPlayers = response;
});
}]);
我现在在这上面浪费了两天,它可能很简单......
更新: 我的应用程序只是从所有比赛中检索数据,获取最后一场比赛的数据并显示参加比赛的球队和日期。然后它应该检索我们团队中所有将要玩它的球员并显示他们的照片和名字。
在不知道您的应用程序要求的情况下,我们只能假设您有正当理由将这两个控制器分开。
如果控制器是兄弟姐妹并且没有范围继承关系,您唯一的选择是通过$rootScope 广播一个事件。然而,这是不好的做法,应该避免。
如果它们有继承关系,child 将使用 $emit(向上)而不是 $broadcast
请阅读
https://docs.angularjs.org/guide/scope
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
了解范围继承。
以下示例(不是我的)http://jsfiddle.net/VxafF/,是一个简单易懂的示例。
$http.get('./lib/data.php?query=getMatches').success(function (response) {
var lastMatch = response.length - 1;
idMatch = response[lastMatch].idMatch;
$scope.date = response[lastMatch].date;
$scope.local = response[lastMatch].local;
$scope.visitor = response[lastMatch].visitor;
$rootScope.broadcast('matchIDEvent', {'idMatch': idMatch});
});
您的 Portraits Controller 然后需要一个新功能:
$rootScope.$on("myEvent",function (event, data) {
$http.get('./lib/data.php?query=getPlayersInMatch&idMatch=' + data.idMatch).success(function (response) {
$scope.players = response;
});
$http.get('./lib/data.php?query=getPlayersOutMatch&idMatch=' + data.idMatch).success(function (response) {
$scope.notConfirmedPlayers = response;
});
});
两个控制器还需要注入 rootScope。