javascript - How to get certain $index values ​​in repeat in angularjs?
阿神
阿神 2017-05-16 13:19:43
0
1
774
<!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返回的数据 */
                    //data = {status:"on",color:{color:"#000"}}
                    /* 有时返回timeout */
                    //data = {status:"timeout",color:{color:"red"}}

                    //push到arr数组,然后通过repeat循环展示
                    /*$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的
            }

            //我的需求就是当点击  "获取timeout的索引" 按钮后  将这些timeout的状态在重新请求,如果请求成功,就会把timeout显示成on.

            //因为循环请求有时会timeout超时

            //所以现在最大的问题就是怎么把 timeout重新请求.然后修改$rootScope.arr数组的相应下标位置,重点就在这里
            
            
        });
    </script>

</body>
</html>
$rootScope.getOffIndex = function(){
                //这里如何获取到全部的status:"timeout"的$index索引值.注意只要timeout的
            }

My requirement is to re-request the timeout status after clicking the "Get timeout index" button. If the request is successful, the timeout will be displayed as on.
Because the cyclic request sometimes times out
So the biggest problem now is how to re-request the timeout. Then modify the corresponding subscript position of the $rootScope.arr array. The focus is here

阿神
阿神

闭关修行中......

reply all(1)
大家讲道理

The comment said that traversing arr is correct,

$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"}}
];

You imagine, I get the object whose attribute is timeout in this array, and its index value in this array

You may want to make a judgment directly at the front desk, you can do it, but it is not recommended

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template