angularjs post web service dropdown
问题描述:
我用Eclipse创建了一个Web应用程序,我使用AngularJS和REST GET Web服务。通过Web服务,我查询我的MySQL数据库中的数据并通过控制器http GET控制器将值发送到我的html页面。 我成功地在我的HTML页面中显示数据库值的下拉列表。现在我想将每个下拉列表中的选定项目发送回java页面,以便我可以创建一个新的查询来生成一些新的数据。但我不知道我能做这第二部分,有人可以帮我吗?angularjs post web service dropdown
在此先感谢!
这是我的HTML页面的一部分
<div id="three" ng-controller="mycontroller">
<table class="table table-hover">
<tbody>
<select ng-model="selecteditem">
<option ng-repeat="item in items">
{{item.itemname}}
</option>
</select>
</tbody>
</table>
<b>You selected: {{selecteditem}}</b>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('showitems', function($scope, $http) {
$http.get('http://localhost:8080/myproject/REST/WebService_items/GetItems').
success(function(data) {
$scope.items = data;
})
.error(function() {
$scope.items = "error in fetching data";
});
});
</script>
答
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="three" ng-controller="mycontroller">
<table class="table table-hover">
<tbody>
<select ng-model="selecteditem">
<option ng-repeat="item in items">
{{item.itemname}}
</option ng-click="callTheFunction(item.itemname})">
</select>
</tbody>
</table>
<b>You selected: {{selecteditem}}</b>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('mycontroller', function($scope, customService) {
$scope.items = [{itemname:'apple'},{itemname:'mango'}]
$scope.callTheFunction=function(itemSelected){
customService.restCallToJava(itemSelected)
}
app.service('customService',['$http', function($http) {
var restCallToJava= function(sensorObj){
return $http.post('/api',sensorObj);
}
}])
</script>
答
<label>example</label>
<md-select id="example" ng-model="vm.list.example" required>
<md-option ng-repeat="unit in vm.list" value="{{example.id}}">{{example.name}}</md-option>
答
感谢两个答案!我的主要问题不是如何显示下拉列表中的项目,我已经完成了这些。我想通过REST Web服务函数将选择的项目发送到我的项目的Java页面,所以我不知道如何在我的HTML页面中编写angularjs代码,以发送推送/发布Web服务URL中的项目。
尽管此代码可能会回答问题,但提供有关如何解决问题和/或解决问题原因的其他上下文会提高答案的长期价值。 – HiDeo