2017-04-10 6 views
0

저는 AngularJS에 처음 왔으며 여기에 질문이 있습니다.

내 CRUD 작업에 $resource을 사용하고 있습니다.

나는 현재 이런 코드, 내가 공장 수준의 API에 대한 특정 URL을 제공하지 않으려는 의미, 내 UserRoleService 제네릭을하고 싶습니다

angular.module("dopAngular.services") 
.factory("UserRoleService", ["$resource", 
    function ($resource) { 
     return $resource("api/UserRoleApi", {}, { 
      query: { method: "GET", isArray: true }, 
      create: { method: "POST" }, 
      get: { method: "GET" }, 
      remove: { method: "DELETE" }, 
      update: { method: "PUT" } 
     }); 

    }]); 

    //below is the code in my controller 
       UserRoleService.query(function (data) { 
       vm.UserRoleLookups = data; 
      }); 

있습니다.

지금

angular.module("dopAngular.services") 
.factory("UserRoleService", ["$resource", 
    function ($resource, url) { 
     return $resource(url, {}, { 
      query: { method: "GET", isArray: true }, 
      create: { method: "POST" }, 
      get: { method: "GET" }, 
      remove: { method: "DELETE" }, 
      update: { method: "PUT" } 
     }); 

    }]); 

내 질문은 내가 내 컨트롤러에해야 할 일입니다, 내 코드를 조금 수정?

답변

0

따라서 직접 $resource을 반환하는 대신 url을 매개 변수로 허용하는 함수로 캡슐화 할 수 있습니다. 이 같은

뭔가 :

myApp.factory('UserRoleService', function($resource) { 
    return { 
    query: function(url) { 
     return $resource(url, {}, { 
     query: { 
      method: "GET", 
      isArray: true 
     }, 
     get: { 
      method: "GET" 
     } 
     }); 
    } 
    } 
}); 

자, 컨트롤러, 당신이 그것을 호출 할 수 있습니다 좋아 :

UserRoleService.query('//httpbin.org').get() 

example fiddle