我正在尝试保存表单(角度材质对话框)数据,保存(插入到DB表格)并将保存的数据注入父元素(背景)。这是我的代码:
//hospital.jsp
<img .... ng-click="showDialog($event)" ....>
//hospitalService.js
app.factory('Hospital', ['$resource',
function($resource) {
return $resource('http://localhost:8081/angular/rest/hospitals/:id', {/*id: '@id'*/}, {
get: {method: 'GET', cache: false, isArray: false},
save: {method: 'POST', cache: false, isArray: false},
update: {method: 'PUT', cache: false, isArray: false}
});
}]);
//hospitalController.js
app.controller('HospitalCtrl', ['$scope', 'Hospital', '$mdDialog',
function ($scope, Hospital, $mdDialog) {
$scope.hospitals = [];
var hospitals = Hospital.query(function() {
console.log(hospitals);
});
...
$scope.showDialog = function($event) {
$mdDialog.show({
controller: DialogController,//HospitalCtrl,
controllerAs: 'hospCtrl',
targetEvent: $event,
clickOutsideToClose: true,
templateUrl: 'partials/modals/hospitalEdit.jsp',
parent: angular.element(document.body),
fullscreen: $scope.customFullscreen,
locals : {
hospitals : $scope.hospital
}
}).then(function(answer) {
$scope.status = 'You said the information was "' + answer + '".';
}, function() {
$scope.status = 'You cancelled the dialog.';
});
};
function DialogController($scope, $mdDialog, hospitals) {
...
$scope.save = function(hospital) {
$scope.save=function(){
$scope.hospital.$save(function(){
console.log("hospital saved");
});
}
};
//-------------------
hospitalEdit.jsp
<form name="hospitalForm" ng-submit="hospitalForm.$valid && hospCtrl.save(hospital)" >
... ... 保存
但是,如果我单击“保存”按钮,则没有任何反应,当我再次单击“保存”按钮时,我会收到“$ scope.hospital。$ save is a function”错误。
提前感谢,
答案 0 :(得分:0)
我改变了这样,它有效。
<form name="hospitalForm" ng-submit="save(hospital)" >
...
<md-button type="submit" class="md-raised md-primary">Save</md-button>
function DialogController(
...
$scope.save = function(hospitalData) {
$scope.hospital.$save(function(hospitalData){
hospital.push(hospitalData);
});
$mdDialog.hide();
};
感谢您的帮助。