http调用ng-repeat

时间:2016-10-05 16:29:31

标签: angularjs angular-promise

我正在尝试遍历一组端点,触发get请求,如果它成功显示状态良好,否则显示状态不佳。
它实际上是对端点的健康检查确保它们正常运行。
测试终点:

    var endpoints = [{
        "description": "test 1",
        "environment": "global",
        "url":"http://www.stackoverflow.com"
    }, {
        "description": "test 2",
        "environment": "global",
        "url": "http://www.letsPretendThisWillFail.com"
    }];

html

<tbody ng-init="getEndpoints()" ng-repeat="e in endpoints" my-repeat-directive>
<tr>
    <td>{{e.description}}</td>
    <td>{{e.environment}}</td>
    <td>
        <i ng-show="canPing(e.url)" class="glyphicon glyphicon-heart green"></i>
        <i ng-show="!canPing(e.url)" class="glyphicon glyphicon-heart red"></i>
    </td>
    <td>
        <button class="btn btn-info btn-sm" role="button" data-toggle="tooltip" data-placement="top" title="Check Service" ng-click="canPing(e.url)">
            <i class="glyphicon glyphicon-refresh"></i> 
        </button>
    </td>
</tr>
</tbody>

控制器:

$scope.canPing = function (e) {

    HealthService
    .testConnection(e)
    .then(success, error);

      function success(response) {
      return true;
      };

    function error(response) {
      return false;
      };
    }

我的困境,当通过ng-click调用时,canPing功能可以自行工作,但是,当它加载页面时,ng-repeat似乎会使我的浏览器断断续续。我确定问题将使用$ q解决,但我不确定如何实现它。

0 个答案:

没有答案