【问题标题】:refactoring "factories" into a $resource将“工厂”重构为 $resource
【发布时间】:2016-01-07 08:44:15
【问题描述】:

您好,我已将以前用于在 api 中编辑记录的功能转换为以下资源:

app.factory("Wine", function($resource){
    return $resource("http://greatwines.8000.com/wines:id", {
        //id as a variable
        id: "@id"
     },
     {
         update: {
             method: "PUT"
         }
    });
});

我现在想通过触发一个带有“葡萄酒”记录的表单来使用它,以便在每种葡萄酒的葡萄酒 ng-repeat 中使用以下 CTA 进行编辑:

   <a href="#/wines/{{wine.id}}" class="btn btn-default">Edit Wine</a>

在我的控制器中,我传递了 "Wine" 资源:

app.controller("editWineCtrl", function ($scope, $http, $routeParams, Wine, $location){


   Wine.get({ id: $routeParams.id }, function(wine){
        $scope.wine = wine;
   });

...

但是,尽管表单 URL 返回 ID:

http://greatwines.8000.com/#/wines/1323

没有字段,即:

div class="margin-top-20">
                    <input type="text" class="form-control" ng-model="wine.year" />
                </div>
                <div class="bold margin-top-20">
                    Grapes
                </div>
                <div class="margin-top-20">
                    <input type="text" class="form-control" ng-model="wine.grapes" />
                </div>

正在填充。我是否以正确的方式使用资源?

【问题讨论】:

  • urlwines:id有错别字,应该是wines/:id
  • 哈!你是明星...谢谢 :-P 我只希望 Angular 像“人”一样解释这些错误。

标签: angularjs ngresource angularjs-ng-resource


【解决方案1】:

网址有错别字

wines:id

应该是

wines/:id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2015-05-13
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多