2014-02-20 59 views
0

我正在用一些服務創建一個新模塊。我這樣註冊了以下服務:

myModule.provider("surveySrv", ["$http", function ($http) { 
    var httpSrv = $http; 
    return { 
    $get: function() { 
     return { 
     getall: function() { 
      return httpSrv.get("/api/survey/all"); 
     }, 
     remove: function (survey) { 
      // ... 
     } 
     }; 
    } 
    }; 
}]); 

,我得到一個錯誤,未能實例由於模塊ilgServices: 未知提供商:$ HTTP

想不通爲什麼。 但是,如果我與工廠方法進行註冊它的工作原理:

myModule.factory("surveySrv", ["$http", function ($http) { 
    return { 
     getall: function() { 
     return $http.get("/api/survey/all"); 
    }, 
    remove: function (survey) { 
     //return $http.get() 
    } 
    }; 
}]) 

我上了GitHub上這樣的解釋:

只能注入東西到$得到了供應商的財產。 $ get屬性是注入你的控制器/指令/服務的東西。提供者的主體主要用於配置塊來改變提供者的行爲。

但有一個問題 - 我該如何「注入事情到$ get屬性」?

回答

1

module#provider在配置階段被調用,只允許注入提供者。

,你可以注入業務爲$ get函數,像這樣:

myModule.provider("surveySrv", function() {  
    return { 
    $get: ["$http", function ($http) { 
     return { 
     getall: function() { 
      return $http.get("/api/survey/all"); 
     }, 
     remove: function (survey) { 
      // ... 
     } 
     }; 
    }] 
    }; 
}); 
相關問題