AngularJSについて質問させてください。複数のShopにShelfとItemがそれぞれネストされて繋がっている以下のようなモデル構成のとき、ngResourceを使ってデータベースにitemを追加(save)する方法についてです。
Shop
└ Shelf
└ Item
Rails側では、レンダリングされたjson "/api/shops/:shop_id/shelfs/:shelf_id/items/:id" を呼び出すAPIに専念させ、
Angular側から、serviceクラスのfactoryメソッドでそれを呼んでいます。
このとき、追加するitemに紐づく動的なshop_idとshelf_idの渡し方がわかりません...
「$scope.newItem = { ... }」の中で指定しているのですがどうやら渡せないみたいで困ってます。
なぜかわかる方、おられませんか...?
《service》
angular.module(’myApp’).factory ’Item’, ($resource) ->
$resource(’/api/shops/:shop_id/shelfs/:shelf_id/items/:id’,
{ shop_id: ’13495’ },
{ shelf_id: ’93’ },
{ update: { method: ’PUT’ }})
《controller》
angular.module(’myApp’).controller "shopEdit", ($scope, Shop, Item) ->
$scope.addItem = (shop, shelf) ->
# 保存
$scope.newItem = { body: shelf.item.body, shop_id: shop.id, shelf_id: shelf.id }
Item.save($scope.newItem)
$scope.newItem = newItem
return
みんなの回答 2 件
「質問させてください」って誰も質問してくれって頼んでないわけだが。
$resouceに渡すパラメーターは第二引数にまとめて一つのオブジェクトで渡す。
$resource(’/api/shops/:shop_id/shelfs/:shelf_id/items/:id’,
{ shop_id: ’13495’,
shelf_id: ’93’,
},
{ update: { method: ’PUT’ }})
関連するトピックス