2014-06-19 72 views
0

我正在嘗試構建服務,以使我的應用程序可以完全訪問單個服務中可用的每個API資源。現在我已經創建了我的主要Angular應用程序模塊和一個名爲APIService的API服務。該服務有一個工廠,將不同的API返回一系列可訪問的Angular $resource。這是代碼。使用Angular Factory提供API調用時未捕獲錯誤

var app = angular.module('MYAPP', ['ngRoute', 'ngSanitize', 'ngResource', 'apiService']); 


    var APIService = angular.module("apiService", ["ngResource"]); 
     APIService.factory("API", function ($resource) { 
       var apiFactory = {}; 

       apiFactory.Alerts = $resource('/WebApi/Alert/:type/:id', {id:'all'}, 
       { 
        systemUpdate: { method: 'GET' }, 
        autoArchive: { method: 'POST', url: '/WebApi/Alert/Template/:type' } 
       }); 

       return apiFactory; 
      }); 

但是,當我嘗試加載頁面時,我在angular.js文件中得到Uncaught Error。我在這裏做錯了什麼?

+0

我猜您使用Chrome,B/C那個討厭的「未捕獲錯誤」消息。 Chrome隱藏了一條真實的,更長的錯誤消息。嘗試使用FireFox,看看你是否得到了更好,更有用的錯誤信息:) –

回答

0

你忘了定義你的對象裏面的函數:

 apiFactory.Alerts = function(){ 

     return $resource('/WebApi/Alert/:type/:id', {id:'all'}, 
      { 
       systemUpdate: { method: 'GET' }, 
       autoArchive: { method: 'POST', url: '/WebApi/Alert/Template/:type' } 
      }); 
    } 
+0

是否必須使用函數?我不希望apiFactory.Alerts是一個函數,我希望它實際上是一個$資源,所以我可以像'API.Alerts.query()' –

+0

那樣訪問$資源。所以返回你$資源,在那裏我寫你可以返回任何你想要的! 我會編輯 – Milad

+0

如果你不想使用一個函數,你必須去其他servises,有很多角度,角度搜索fo搜索。 但工廠將完成這項工作 – Milad

相關問題