我有一些Angular代碼,我似乎無法調試。加載頁面在導航下拉欄中完美呈現我的所有羣組。當我點擊其中一個組時,會調用messagesIndex函數,並且來自該組的所有消息都將正常顯示,此時一切工作正常。問題是,當我去點擊另一個組,或者甚至是同一個組時,messagesIndex函數不會再次加載。無法在AngularJS中調用某個函數兩次
我花了很多時間試圖調試這個問題無濟於事,但我已經精確地確定了問題發生的地方,這裏似乎沒有任何問題可以找到。
我用$ scope.groupID替換了messagesIndex函數的內容,並將結果與隨機數一起輸出。 groupID與隨機數一起顯示,每次我點擊不同的組時都會出現不同的ID和隨機數,這意味着JADE/HTML運行正常並且功能標題是黃金。
然後,我接着使用原始的messagesIndex函數,並在函數的開始和結束處添加了$ scope.random1和scope.random2,並在函數的開始和結束處添加了隨機數,無論是在HTTP請求之前還是之後。當我第一次調用該函數時,兩個隨機數都會生成並被輸出,雖然http請求是異步的,但所有的值都是正常返回的,暗示該函數完全調用,除非在請求成功後拖動HTTP請求響應?然後,當我通過點擊另一組來再次打電話時。沒有任何隨機數發生了變化,表明該函數根本沒有調用,或者在執行第一行代碼之前停止。
坦白地說,我很難過。代碼看起來基本與許多正在運行的HTTP請求相同,但由於某種原因,我無法將其調用兩次。
下面是AngularJS代碼:
angular.module('angular-client').controller('GroupMeCtrl',
['$scope', '$http', '$templateCache', 'Auth', function($scope, $http, $templateCache) {
$scope.groupsIndex = function(){
$http({
method: 'GET',
url: 'https://api.groupme.com/v3/groups?token=' + token,
cache: $templateCache
}).
success(function(data, status) {
$scope.groupsIndex = data.response;
$scope.stat = status;
}).
error(function(data, status, headers, config) {
$scope.groupsIndex = data.response;
$scope.stat = status || "Request failed";
});
};
$scope.messagesIndex = function($groupID){
$http({
method: 'GET',
url: 'https://api.groupme.com/v3/groups/' + $groupID + '/messages?token=' + token,
cache: $templateCache
}).
success(function(data, status) {
$scope.messagesIndex = data;
$scope.stat = status;
}).
error(function(data, status) {
$scope.messagesIndex = data.response;
$scope.stat = status || "Request failed";
});
};
}]);
及以下這裏是我的JADE:
li.dropdown
a.dropdown-toggle(href='#', data-toggle='dropdown')
| Groups
b.caret
ul.dropdown-menu
li(ng-repeat="group in groupsIndex")
a(ng-click="messagesIndex(group.id)") {{group.name}}
.message-box(ng-repeat="message in messagesIndex.response.messages")
.avatar-container
img(src="{{message.avatar_url}}").avatar
.text
.user-name
{{message.name}}
span
{{message.text}}