2016-10-27 2 views
0

특정 비 ID 필드를 검색하려고하면 비표준 where 절이 필요합니다. 내가 필요로하는 엔드 포인트는 다음과 같습니다 Angular ngResource를 사용하여 비표준 API에 문의하는 방법

http://127.0.0.1:4001/api/testusers/findOne?userName=Anton

그래서이 나에게 열 (사용자 이름) = '안톤'는 testusers 테이블의 첫 번째 레코드를 찾을 수 있습니다.

내 표준 서비스는 다음과 같습니다

angular. 
 
    module('shared.testUser'). 
 
    factory('TestUser', ['$resource', 
 
    function($resource) { 
 
     return $resource('http://127.0.0.1:4001/api/testusers/:id', {id:'@id'},//parameters 
 
      { 
 
       update: { 
 
         method: 'PUT' // To send the HTTP Put request when calling this custom update method. 
 
       } 
 
        
 
      }); 
 
    } 
 
    ]);

내 호출 기능은 다음과 같습니다

self.checkUsersEntryDirection = function(){ //NOT WORKING 
 
     self.testuser = TestUser.get({ username: 'anton' }, function() { 
 
     console.log(angular.toJson(self.testuser)); 
 
     }); // get() returns a single entry 
 
    }

은 분명이 작동하지 않습니다 나는 표준 접근법을 사용할 수 없다. 아무도 이것이 어떻게 달성 될 수 있다고 생각할 수 있습니까?

답변

0

당신은 보조 공장 TestUserByName를 작성하고 다음과 같이 변경 할 수 있습니다 :

angular. 
    module('shared.testUser'). 
    factory('TestUserByName', ['$resource', 
    function($resource) { 
     return $resource('http://127.0.0.1:4001/api/testusers/findOne?userName:username', null, 
      { 
       update: { 
         method: 'PUT' // To send the HTTP Put request when calling this custom update method. 
       } 

      }); 
    } 
    ]); 
0

전화 두 개의 매개 변수를 사용하여 get 액션에있어서, 상기 id 매개 변수는 기본 및 username 우선합니다

var params = {id: "findOne", username: "anton"}; 

self.checkUsersEntryDirection = function(){ 
    self.testuser = TestUser.get(params, function() { 
    console.log(angular.toJson(self.testuser)); 
    }); // get() returns a single entry 
} 

을 매개 변수가 쿼리 문자열로 추가됩니다. 워드 프로세서

:

매개 변수 객체의 각 키 값은 첫번째 경우 URL 검색 쿼리에 존재하고 초과 키가 추가 된 후 URL 템플릿에 바인딩?.

템플릿 /path/:verb 및 매개 변수 {verb:'greet', salutation:'Hello'}의 결과는 /path/greet?salutation=Hello입니다.

-AngularJS ngResource $resource Service API Reference

관련 문제