。$保存不工作角度

时间:2016-08-04 09:43:41

标签: angularjs ionic-framework

app.factory('Shipment', function($resource) {
  return function(auth_token){
    return $resource(basePath + "shipments/:id", { id: '@_id' }, {
      query:       { method: 'GET', params: {auth_token: auth_token}, isArray:true                                  },
      update:      { method: 'PUT'                                                                                  }
    });
  }
});

控制器

$scope.shipment = new Shipment($scope.shipment)
$scope.shipment.$save(function() {
  debugger
});

它给了我这个错误:

$scope.shipment.$save is not a function

查询和获取工作正常。

2 个答案:

答案 0 :(得分:1)

如下所示声明你的工厂。

do ->
  angular.module('yourModule').factory('prefixShipment', ['$resource', function($resource){
    $resource(basePath + "shipments/:id", { id: '@_id' }, {
      update: {
        method: 'PUT'
      }
    })
  }])

现在保存使用下面的代码段。

shipment = new prefixShipment({shipment: shipment})
shipment.save({},function(response){
    //your code      
})

忘记记录。

prefixShipment.get({ id: $stateParams.shipment_id } , function(data{
  $scope.booking = data.booking
});

答案 1 :(得分:0)

不要使用Constructor !!试试这个:

$scope.shipment = Shipment($scope.shipment)
$scope.shipment.$save(function() {
  debugger
});