我有一个在控制器加载时调用的函数(对应于tab-plan.html的PlanCtrl,其中填充了手风琴列表,见下文):
$scope.initialize = function() {
if ($cookies.token) {
$http.defaults.headers.common.Authorization = 'Token ' + $cookies.token;
}
var url = "https://127.0.0.1:8000/api/exercises/get_exercise/";
var memory_url = "https://127.0.0.1:8000/api2/games/get_game/";
$http({
url: memory_url,
method: 'GET',
headers: {
'X-CSRFToken': $cookies['csrftoken'],
'Content-Type': 'application/json; charset=utf-8'
}
})
.success(function(response) {
alert("suc 1")
$scope.initial_data_memory = response;
console.log($scope.initial_data_memory);
angular.forEach($scope.initial_data_memory, function(item) {
$scope.game_type = item.game_type;
//check the exercise date
$scope.current_date_memory = item.current_date;
$scope.days = Plan.all();
//Iterate over the list
angular.forEach($scope.days, function(value1, key) {
//iterate over the list of exercises
angular.forEach(value1.exercises, function(value2, key) {
// Check if the fetched exercise id is same as current exercise from the frontend list and check if the fetched date is today
if (value2.id == $scope.game_type && $scope.today === $scope.current_date_memory) {
// If the fetched date is todat i.e. exercise was rated today: set the flag for watchedTodat to true
value2.watchedToday = true;
}
});
});
});
});
$http({
url: url,
method: 'GET',
headers: {
'X-CSRFToken': $cookies['csrftoken'],
'Content-Type': 'application/json; charset=utf-8'
}
})
.success(function(response) {
alert("suc 2")
$scope.initial_data = response;
console.log($scope.initial_data);
angular.forEach($scope.initial_data, function(item) {
$scope.exercise_type = item.exercise_type;
//check the exercise date
$scope.current_date = item.current_date;
$scope.days = Plan.all();
//Iterate over the list
angular.forEach($scope.days, function(value1, key) {
//iterate over the list of exercises
angular.forEach(value1.exercises, function(value2, key) {
// Check if the fetched exercise id is same as current exercise from the frontend list and check if the fetched date is today
if (value2.id == $scope.exercise_type && $scope.today === $scope.current_date) {
// If the fetched date is today i.e. exercise was rated today: set the flag for watchedTodat to true
value2.watchedToday = true;
}
});
});
});
});
};
它有两个http调用(顺序无所谓),当控制器第一次加载时只执行第一次调用,当第二次加载时,第二次http调用执行/成功。
为什么会这样?
更新
我在两个成功部分都设置了警报:两个都进行了HTTP调用,但最后一个语句value2.watchedToday = true;
设置了一个布尔值以启用复选标记,仅设置第二个http调用。见图像波纹管(理想情况下应该有复选标记Best Stretch和Buttterfly反向):
HTML:
<ion-list>
<div ng-repeat="day in days"><br>
<div class="item item-icon-left" ng-click="toggleGroup(day)" ng-class="{active: isGroupShown(day)}">
<i class="icon icon-accessory" ng-class="isGroupShown(day) ? 'ion-minus' : 'ion-plus'"></i>
{{day.name}}
</div>
<a class="item item-icon-left item-accordion" ng-show="isGroupShown(day)" ng-repeat="exercise in day.exercises" type="item-text-wrap"
href="#/tab/plan/{{exercise.id}}">
{{exercise.name}}
<!-- Trial LED -->
<span style="float:right;"><i ng-show="{{exercise.watchedToday}}==true" class="ion-checkmark-round"></i></span>
</a>
</div>
</ion-list>
路线:
.state('tab.plan', {
url: '/plan',
resolve: {
authenticated: ['djangoAuth', function(djangoAuth){
return djangoAuth.authenticationStatus();
}],
},
views: {
'tab-plan': {
templateUrl: '/templates/tab-plan.html',
controller: 'planCtrl'
}
},
cache: false
})
.state('tab.simon', {
url: '/plan/8',
resolve: {
authenticated: ['djangoAuth', function(djangoAuth){
return djangoAuth.authenticationStatus();
}],
},
views: {
'tab-plan': {
templateUrl: '/templates/simon.html',
controller: 'simonCtrl',
data: {
css: '/css/simon.css'
}
}
},
cache: false
})
...
.state('tab.exercise', {
url: '/plan/:exerciseId',
resolve: {
authenticated: ['djangoAuth', function(djangoAuth){
return djangoAuth.authenticationStatus();
}],
},
views: {
'tab-plan': {
templateUrl: '/templates/video.html',
controller: 'videoCtrl'
}
},
cache: false
})
Best Stretch,Butterfly,Squat,Plank,Push up,Side plank共享控制器(videoCtrl + video.html)以及所有其他控制器和模板。