入力データを取得してリソースに変換し、保存してローカルキャッシュに挿入するサービスの作成機能があります。
create = (data, success, error) ->
throw new Error("Unable to create new user, no company id exists") unless $resource_companies.id()
# create the resource
new_model = new resource(data)
# manually add the company id into the payload (TODO: company id should be inferred from the URL)
new_model.company_id = $resource_companies.id()
# do the save and callbacks
new_model.$save( () ->
list.push(new_model)
success.call(@) if success?
, error)
私の問題は、リスト変数を監視する ng:repeat が更新されないことです。私が知る限り、これは AngularJS の外部で発生していないため、最新の状態に保つために $scope.$apply() は必要ありません (実際、ダイジェストをトリガーしようとすると、既に進行中のダイジェストが取得されます)。エラー)
二重に奇妙なのは、このまったく同じパターンを他の場所で問題なく使用していることです。
リスト配列にアクセスするためにコントローラーが使用するコードは次のとおりです(これはサービスにあります)
# refreshes the users in the system
refresh = (cb) -> list = resource.query( () -> cb.call(@) if cb? )
コントローラーで:
$scope.users = $resource_users.refresh()