如何从 angularjs 函数调用 grails 控制器
how to call grails controller from an angularjs function
我有一个 grails 应用程序,我正在尝试使用 angular 进行前端工作。
假设我的页面上有一个按钮,比如说,'Add Request'。单击此按钮后,我将调用 angular 函数。但是我无法弄清楚,在这个 angular 函数中,我如何调用这个 grails 控制器中的 'Grails controller' 和方法。
// add Request data
$scope.addRequest = (function addRequest() {
console.log('$scope.request ' +$scope.request);
var requestInfo = new String($scope.request);
requestInfo.$save();
$scope.request = {};
});
有几种不同的方法可以做到这一点。您可以提交表单,也可以执行 Ajax 请求。这两种方法我都用过,所以这取决于你的整体应用程序结构是如何设置的。
AJAX
// assuming you have your various form elements bound to $scope.form = {};
// e.g. <input ng-model="form.something">
var promise = return $http.post("/controller/action",$scope.form).
success(function(data) {
}).error(function(data) {
});
提交表格
jQuery('#formid').submit(); // Using Jquery to simply submit the form
您也可以只放置一个提交类型的输入按钮
并在标签中包含您的控制器和操作。
我有一个 grails 应用程序,我正在尝试使用 angular 进行前端工作。
假设我的页面上有一个按钮,比如说,'Add Request'。单击此按钮后,我将调用 angular 函数。但是我无法弄清楚,在这个 angular 函数中,我如何调用这个 grails 控制器中的 'Grails controller' 和方法。
// add Request data
$scope.addRequest = (function addRequest() {
console.log('$scope.request ' +$scope.request);
var requestInfo = new String($scope.request);
requestInfo.$save();
$scope.request = {};
});
有几种不同的方法可以做到这一点。您可以提交表单,也可以执行 Ajax 请求。这两种方法我都用过,所以这取决于你的整体应用程序结构是如何设置的。
AJAX
// assuming you have your various form elements bound to $scope.form = {};
// e.g. <input ng-model="form.something">
var promise = return $http.post("/controller/action",$scope.form).
success(function(data) {
}).error(function(data) {
});
提交表格
jQuery('#formid').submit(); // Using Jquery to simply submit the form
您也可以只放置一个提交类型的输入按钮 并在标签中包含您的控制器和操作。