2015-09-09 27 views
0

現在我試圖顯示從服務到controller..When我想顯示錯誤的不低於working.The返回的響應是,我試圖如何在服務向角控js中的控制器返回錯誤後在頁面上顯示錯誤?

var app=angular.module('httpExample',[]); 
app.controller("FetchController",["$scope","clickevent",function($scope,clickevent){ 
    clickevent.fetch().then(function(response){ 
     $scope.req=response; 
     console.log(angular.toJson(response)); 
    }).error(function(error){ 
      console.log(error); 
    }); 

}]); 
app.service("clickevent",['$http',function($http){ 

     var clickevent={ 
       fetch:function(){ 
        var prom=$http({method:"GET",url:"http://www.w3schools.com/angular/customers.php"}). 
        success(function(response){ 
         return response; 
        }). 
        error(function(err){ 
         return err; 
        }); 
        return prom; 
       } 
     }; 
     return clickevent; 
    }]); 
+1

您有拼寫錯誤'function'本着'})錯誤(溫控功能(錯誤){'(6號線) – Nalaka526

+0

樂施會sorry..Even但其'clickevent.fetch'中沒有給出任何錯誤 –

+0

返回承諾? – Jorg

回答

0

承諾的代碼沒有error()功能。他們有一個catch()函數。 success()error()特定於由$ http返回的承諾,而那些已棄用函數不允許鏈接,如then()catch()允許。

這裏是你的代碼應該如何看起來像:

var app = angular.module('httpExample',[]); 

app.controller("FetchController",["$scope", "clickevent", function($scope, clickevent){ 
    clickevent.fetch().then(function(data) { 
     $scope.req = data; 
     console.log(angular.toJson(data)); 
    }).catch(function(error) { 
     console.log(error); 
    }); 
}]); 

app.service("clickevent",['$http', '$q', function($http, $q){ 
    var clickevent = { 
     fetch: function() { 
      var prom = $http({method:"GET",url:"http://www.w3schools.com/angular/customers.php"}). 
       then(function(response) { 
        return response.data; 
       }). 
       catch(function(response) { 
        return $q.reject(response.data); 
       }); 
      return prom; 
     } 
    }; 
    return clickevent; 
}]); 
+0

即使iam使用它,只有在出現錯誤時纔會顯示響應 –

+0

那麼,當代碼響應錯誤時,代碼顯示響應的主體你想要顯示什麼呢? –

+0

我正在改變POST的方法來檢查是否發生錯誤......所以當方法發生了什麼錯誤時ged,該錯誤必須顯示在頁面上。是否有可能 –

相關問題