2017-04-05 42 views
0

我想要生成angularjs使用ngResource這個網址:如何生成ngResource網址有兩個參數

http://url_api/contact/58dc70f18e029b1338a97abc/address/58e3e3988e029a1aec5ad465

而是我得到這個:

http://url_api/contact/58dc70f18e029b1338a97abc/address?address_id=58e3e3988e029a1aec5ad465

這是我廠

factory('Addresses', function($resource) { 
    return $resource(url_base + 'contact/:id/address',{},{ 
     get: { 
      method: 'GET', 
      isArray: true, 
      url: url_base + 'contact/:id/address' 
     }, 
     save:{ 
      method: 'POST' 
     }, 
     delete:{ 
      method: 'DELETE', 
      url: url_base + 'contact/:id/address/:addressId', 
      // params:{ 
      //  id: '@_id', 
      //  addressId: '@_addressId' 
      // } 
     } 
    }); 
}) 

,這是我以後怎麼稱呼它

Addresses.delete({id:$scope.id_contact,address_id:address_id_delete}) 

請任何人都可以幫助我嗎?我很着急

回答

1

您需要在空白對象中提及您的params。就像這樣:

factory('Addresses', function($resource) { 
    return $resource(url_base + 'contact/:id/address',{ 
     id: '@id', 
     addressId: '@addressId' 
    },{ 
     get: { 
      method: 'GET', 
      isArray: true, 
      url: url_base + 'contact/:id/address' 
     }, 
     save:{ 
      method: 'POST' 
     }, 
     delete:{ 
      method: 'DELETE', 
      url: url_base + 'contact/:id/address/:addressId', 
      // params:{ 
      //  id: '@_id', 
      //  addressId: '@_addressId' 
      // } 
     } 
    }); 
}) 
0

在網址模板中的參數需要在調用匹配參數:

delete:{ 
     method: 'DELETE', 
     url: url_base + 'contact/:id/address/:addressId', 
     // params:{ 
     //  id: '@_id', 
     //  addressId: '@_addressId' 
     // } 
    } 
//WRONG address_id 
//Addresses.delete({id:$scope.id_contact,address_id:address_id_delete}) 

//RIGHT addressId 
Addresses.delete({id:$scope.id_contact,addressId:address_id_delete})