easy url

with $resource

sample
  • 1번 그룹 조회 (id)
    •  '/groups/1.json'
  • 그룹 전체 설정 (collection)
    • '/groups/setting.json'
  • 그룹 1번의 멤버정보 (member)
    • '/groups/1/members.json'

use $http?



  • $http({method: 'GET', url: '/groups/1.json'})
  • $http({method: 'GET', url: '/groups/setting.json'})
  • $http({method: 'GET', url: '/groups/1/members.json'})


duplicate!!!

use $resource!

url with parameters



 '/groups/:collection_action/:id/:member_action.json'

...
.factory('groupService', function ($resource) {
    return $resource('/groups/:collection_action/:id/:member_action.json', {
... 

sample #1


'/groups/:collection_action/:id/:member_action.json'

1번 그룹 조회

:id => 1

'/groups/ :collection_action/ :id/:member_action.json'

/ groups /1.json

sample #2


'/groups/:collection_action/:id/:member_action.json'

그룹 전체 설정

:collection_action => "setting"

'/groups/:collection_action/:id/:member_action.json'

/groups/setting.json

sample #3


'/groups/:collection_action/:id/:member_action.json'

그룹 1번의 멤버정보

:id => 1, :member_action => "members"

'/groups/:collection_action/:id/:member_action.json'

/groups/1/members.json

code

angular.module('bbNgApp')
  .factory('groupService', function ($resource) {
    return $resource('/groups/:collection_action/:id/:member_action.json', {
      id: '@id'
    }, {
      update: {
        method: "PUT"
      },      setting: {        method: "GET",        params: {          collection_action: "setting"        }      },
      members: {
        method: "GET",
        params: {
          member_action: "members"
        },
        isArray: true
      }    });
  }); 

link






$resource url tip

By ChungSub Kim

$resource url tip

angularjs의 $resource 사용시 url 작성 tip 입니다.

  • 1,386