[英]Angular ajax call after clicking on radio button
我想在管理控制台的应用程序中加入CRUD部分。 我正在使用mongodb,spring,bootstrap和angular。 我在左侧有一个单选按钮列表,其中包含集合的名称(集合的数量是可变的),而在右边的数据表格中包含该数据表中尚未实现的文档。 逻辑是:管理员单击左侧的单选按钮,此后,我想使用单选按钮的名称向服务器发送ajax调用,响应将包含该集合中的文档。
到现在为止,我有:
jsp内容:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="/WEB-INF/views/includes/script/header.jsp"/>
<script type="text/javascript" src="/res/custom_script/admin/main.js"> </script>
<script type="text/javascript" src="/res/custom_script/admin/common_admin_all.js"></script>
<script type="text/javascript">
angular.module("mainAdmin", [])
.controller("collectionsArray", function($scope) {
$scope.colnames = ${collectionNames};
});
</script>
<html>
<head>
<title>Main admin</title>
</head>
<body ng-app="mainAdmin">
<div class="container-fluid">
<p class="logout_paragraph">Logged as <strong>${pageContext.request.userPrincipal.name}</strong> | <a id="logout_link" onclick="formSubmit()">Logout</a></p>
<form action="/logout" method="post" id="logoutForm" style="display: none;">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
<div class="jumbotron">
<h2>Welcome !</h2>
</div>
<div id="divchkCollections" class="admin_left_menu pre-scrollable col-md-2">
<div ng-controller="collectionsArray" id="chkCollections" class="admin_collection_list radio">
<h4>Collections</h4>
<label ng-repeat="colname in colnames">
<input type="radio" name="chkCollectionsRadio" value="{{colname}}" class="radio-button"> {{colname}}
</label>
</div>
</div>
<div id="admin_data_table" class="col-md-10">
</div>
</div>
</body>
</html>
我怎样才能从上面做出逻辑? 要在管理员单击时将带有广播名称的Ajax呼叫发送到服务器? 名称列表在collectionNames中,并且在leght和名称中是可变的。
谢谢。
您需要在单选按钮代码中添加一个ng-change参数,该代码调用JavaScript代码中的函数并引用已更改的单选按钮。
使用指令ng-change将功能绑定到单选按钮,然后使用ng-model将单选按钮的值绑定到变量:
单击单选按钮时,将调用该函数,并且单选按钮的值位于您在ng-model中绑定的变量中。
<input type="radio" name="radioName" value="red" ng-model="valueSelected" ng-change="onClick()">
然后在您的控制器中,在您分配给单选按钮的函数中进行ajax调用:
<script type="text/javascript">
angular.module("mainAdmin", [])
.controller("collectionsArray", function($scope) {
$scope.colnames = ${collectionNames};
//function binded to the radio button on change
$scope.onClick = function (){
//alert to test that you send the radio button value
alert($scope.valueSelected);
//ajax call
$.ajax(
{url: "test",
success: function(data){
}});
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.