我将此工厂定义为调用rest方法:
(function() {
'use strict';
angular.module('rgh').factory('Charge', Charge);
function Charge($resource) {
return $resource('rgh/charge/charge', {}, {
'save' : {
url : 'rgh/charge/charge',
method : 'POST'
}
});
}
})();
和RestContrller
:
@RestController
@RequestMapping("/rgh/charge/charge")
public class ChargeResource {
@Autowired
private IChargeService chargeService;
@PostMapping
public long save(@RequestBody ChargeViewModel viewModelEntity) {
return chargeService.save(ModelMapper.map(viewModelEntity, Charge.class));
}
}
你可以看到save方法返回已保存对象的id,现在我想在角度控制器中得到这个返回的id:
(function() {
'use strict';
angular
.module('rgh')
.controller('ChargeDetailController', ChargeDetailController);
function ChargeDetailController(Charge) {
var vm = this;
vm.save = save;
function save() {
vm.isSaving = true;
Charge.save(vm.charge, onSaveSuccess, onSaveError);
}
var onSaveSuccess = function (result) {
vm.isSaving = false;
console.log(result);
};
var onSaveError = function () {
vm.isSaving = false;
};
}
})();
但是当我检查控制台时,我看到$promise
对象而不是实际ID。
答案 0 :(得分:0)
您可以按如下方式更改Angular Controller中的save
方法:
function save() {
vm.isSaving = true;
Charge.save(vm.charge).$promise
.then(function (result) {
console.log(result);
})
.catch(function (error) {
// Error
})
.finally(function() {
vm.isSaving = false;
});
}