javascript - angularjs怎么獲取repeat里的某些$index值?
問題描述
<!DOCTYPE html><html ng-app='app'><head> <title></title> <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js'></script></head><body> <ul><li ng-repeat='d in arr' > <span ng-bind='$index'></span> <span ng-bind='d.status' ng-style='d.color'></span></li> </ul> <a href='javascript:void(0)' ng-click='getOffIndex()'>獲取timeout的索引</a> <script type='text/javascript'>var app = angular.module('app',[]);app.run(function($rootScope){ for(var i=0; i<10; i++){$http({ method : 'JSON', url : '/xxxx/data'}).then(function(data){ /* data返回的數(shù)據(jù) */ //data = {status:'on',color:{color:'#000'}} /* 有時返回timeout */ //data = {status:'timeout',color:{color:'red'}} //push到arr數(shù)組,然后通過repeat循環(huán)展示 /*$rootScope.arr = [{status:'on',color:{color:'#000'}},{status:'on',color:{color:'#000'}},{status:'timeout',color:{color:'red'}},{status:'on',color:{color:'#000'}},{status:'timeout',color:{color:'red'}},{status:'on',color:{color:'#000'}},{status:'on',color:{color:'#000'}},{status:'on',color:{color:'#000'}},{status:'on',color:{color:'#000'}},{status:'on',color:{color:'#000'}},{status:'timeout',color:{color:'red'}} ];*/ $rootScope.arr.push(data);},function(){}); } $rootScope.getOffIndex = function(){//這里如何獲取到全部的status:'timeout'的$index索引值.注意只要timeout的 } //我的需求就是當(dāng)點擊 '獲取timeout的索引' 按鈕后 將這些timeout的狀態(tài)在重新請求,如果請求成功,就會把timeout顯示成on. //因為循環(huán)請求有時會timeout超時 //所以現(xiàn)在最大的問題就是怎么把 timeout重新請求.然后修改$rootScope.arr數(shù)組的相應(yīng)下標(biāo)位置,重點就在這里}); </script></body></html>
$rootScope.getOffIndex = function(){//這里如何獲取到全部的status:'timeout'的$index索引值.注意只要timeout的 }
我的需求就是當(dāng)點擊 '獲取timeout的索引' 按鈕后 將這些timeout的狀態(tài)在重新請求,如果請求成功,就會把timeout顯示成on.因為循環(huán)請求有時會timeout超時所以現(xiàn)在最大的問題就是怎么把 timeout重新請求.然后修改$rootScope.arr數(shù)組的相應(yīng)下標(biāo)位置,重點就在這里
問題解答
回答1:評論說遍歷arr是對的,
$rootScope.arr = [ {status:'on',color:{color:'#000'}}, {status:'on',color:{color:'#000'}}, {status:'timeout',color:{color:'red'}}, {status:'on',color:{color:'#000'}}, {status:'timeout',color:{color:'red'}}, {status:'on',color:{color:'#000'}}, {status:'on',color:{color:'#000'}}, {status:'on',color:{color:'#000'}}, {status:'on',color:{color:'#000'}}, {status:'on',color:{color:'#000'}}, {status:'timeout',color:{color:'red'}}];
你想象成,我獲得這個數(shù)組中,屬性為timeout的對象,他在這個數(shù)組中的index值
你可能是想在前臺直接判斷,可以做,不推薦這樣做
相關(guān)文章:
1. mysql - 數(shù)據(jù)庫建表方面的問題?2. javascript - angularJS指令如何暴露API給外面的controller使用?3. mysql - 我用SQL語句 更新 行的時候,發(fā)現(xiàn)全部 中文都被清空了,請問怎么解決?4. python - xpath提取網(wǎng)頁路徑?jīng)]問題,但是缺失內(nèi)容?5. javascript - IOS微信audio標(biāo)簽不能通過touchend播放6. javascript - 求教各位,本地HTML頁面怎么在DIV中嵌套服務(wù)器上的頁面內(nèi)容?不用iframe。7. python-mysql Commands out of sync8. java中這個頁面默認是utf-8編碼的,1輸出亂碼可以理解,可是2就不理解了?9. [前端求職必看]前端開發(fā)面試題與答案精選_大綱10. docker不顯示端口映射呢?
