Angular.JS 내장 서비스$http 데이터베이스 추가 삭제 사용 튜 토리 얼
3296 단어 angular.js$http첨삭 하여 고치다
1.$http 로 MySQL 데이터 조회
angular.module('app',[])
.controller('MyCtrl',function ($scope,$http) {
$http.get('http://127.0.0.1:80/user/getUsers')
.success(function (resp) {
console.log(resp);
})
.error()
//jQuery
/*$.get('url',function (data) {
});*/
})대응 하 는 배경 자바 코드:
public void getUsers(){
List<User> users = User.dao.find("select * from t_user");
renderJson(Users);
}2.$http 데이터 에 대한 추가 삭제 실현(1)$http 매개 변수 전송 요청
(2)MySQL 데이터 추가 삭제
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS $http</title>
<link rel="stylesheet" href="css/foundation.min.css" rel="external nofollow" >
<style type="text/css">
html,body{font-size:14px;}
</style>
</head>
<body style="padding:10px;" ng-app="app">
<div ng-controller="MyCtrl">
<input type="text" ng-model="id">
<input type="text" ng-model="name">
<button class="button" onclick="addUser()"> </button>
<button class="button" onclick="delUser()"> </button>
</div>
</body>
<script src="js/angular.min.js"></script>
<script src="app.js"></script>
</html>
angular.module('app', [])
.controller('MyCtrl', function ($scope, $http) {
$scope.id=" ";
$scope.name=" ";
$scope.addUser = function () {
$http.post('http://127.0.0.1:80/user/addUser',{id:$scope.id, name:$scope.name})
.success(function (resp) {
if(resp.success){
alert(" ");
}
})
}
$scope.delUser = function () {
$htp.post('http://127.0.0.1:80/user/delUser',{id:$scope.id})
.success(function () {
if(resp.success){
alert(' ');
}
})
}
})배경 자바 코드:
public void addUser(){
String id = getPara("id");
String name = getPara("name");
User user = new User();
boolean isok = false;
if(id != null && id.equals("")){
isok = user.set("id",id).set("name",name).update();
}else{
isok = user.set("name",name).save();
}
renderJson("seccess",isok);
}
public void delUser(){
String id = getPara("id");
boolean isok = User.dao.deleById(id);
renderJson("seccess",isok);
}총결산이상 은 이 글 의 모든 내용 입 니 다.본 논문 의 내용 이 여러분 에 게 Angular.js 를 배우 거나 사용 하 는 데 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.