2017-01-23 85 views
0

獲取API的名字,我有一個角指令如何angularjs指令

mainApplicationModule 
    .directive('usSpinner2', ['$http', '$rootScope','$location','$state', function($http, $rootScope,$location,$state) { 
     return { 
      link: function(scope, elm, attrs) { 
       $rootScope.spinnerActive = false; 

       scope.isLoading = function() { 
        return $http.pendingRequests.length > 0; 
       }; 
       scope.$watch(scope.isLoading, function(loading) { 
        $rootScope.spinnerActive = loading; 
        if (loading) { 
         elm.removeClass('ng-hide'); 
        } else { 
         elm.addClass('ng-hide'); 
        } 
       }); 
      } 
     }; 
    }]) 

,當我使用$ HTTP服務微調開始對服務器請求,並在響應到達客戶端停止。

我在客戶端

$http.get(baseUrl+'filename/apiname').success(function(res,req){ 
    //code 
}) 

使用這樣的$ HTTP服務在服務器端filename.js

router.get('/apiname',function(req,res){ 
    //code 
}) 

我想這是在要求對服務器數據當前apiname上面的指令。找了幾個小時後我找不到答案。我該怎麼做呢?

回答

0

根據pending requests

PendingRequest的定義:

配置的對象Array爲當前未決的請求。這主要是爲了用於調試目的。

所以。 $http.pendingRequests返回一個配置對象的數組,

所以,你可以嘗試

$http.pendingRequests[0].url來得到當前的API網址。

你可以嘗試這樣的事情。

$scope.isLoading = function() { 
    return $http.pendingRequests.length > 0; 
}; 


$scope.$watch($scope.isLoading, function(loading) { 
     if (loading) { 
     console.log($http.pendingRequests[0].url) 
     } else { 
     console.log($http.pendingRequests) 
     } 
}); 

Here is a DEMO

+0

您的解決方案不工作我得到的不確定 – AMahajan

+0

誤差不能讀取屬性「URL」請一次粘貼您最新的代碼。檢查演示鏈接。打開控制檯併發出JSON請求並單擊提取。 – Sravan

+0

抱歉,我在錯誤的地方粘貼了主線。有效。謝謝 – AMahajan